-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution173.java
More file actions
35 lines (32 loc) · 804 Bytes
/
Copy pathsolution173.java
File metadata and controls
35 lines (32 loc) · 804 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.ArrayList;
import java.util.List;
import java.util.Stack;
public class solution173 {
Stack<solution96_treenode> stack;
solution96_treenode t;
public solution173(solution96_treenode root){
stack = new Stack<>();
t =root;
while(t!=null)
{
stack.push(t);
t = t.left;
}
}
/** @return the next smallest number */
public int next() {
solution96_treenode node = stack.pop();
int val = node.val;
node = node.right;
while(node!=null)
{
stack.push(node);
node = node.left;
}
return val;
}
/** @return whether we have a next smallest number */
public boolean hasNext() {
return stack.isEmpty();
}
}