-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinary_173.java
More file actions
35 lines (29 loc) · 746 Bytes
/
Copy pathBinary_173.java
File metadata and controls
35 lines (29 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.ArrayDeque;
public class Binary_173 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
ArrayDeque<TreeNode> st = new ArrayDeque<>();
private void pushAll(TreeNode root) {
while (root != null) {
st.push(root);
root = root.left;
}
}
public Binary_173(TreeNode root) {
pushAll(root);
}
/** @return whether we have a next smallest number */
public boolean hasNext() {
return !st.isEmpty();
}
/** @return the next smallest number */
public int next() {
TreeNode tmp = st.pop();
pushAll(tmp.right);
return tmp.val;
}
}