-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBSTIterator.java
79 lines (68 loc) · 1.92 KB
/
BSTIterator.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package leetcode.algo.design.leet_zh_173;
import leetcode.algo.tree.base.TreeNode;
import leetcode.algo.tree.leet_zh_1167.Codec;
import java.util.Stack;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class BSTIterator {
/*
*
* 173. 二叉搜索树迭代器
* 利用二叉树的迭代方式的中序遍历,保存左子链,从而使用O(h)的内存。
*
* 执行用时 : 134 ms, 在Binary Search Tree Iterator的Java提交中击败了81.31% 的用户
* 内存消耗 : 59.4 MB, 在Binary Search Tree Iterator的Java提交中击败了81.28% 的用户
* */
private Stack<TreeNode> stack;
public BSTIterator(TreeNode root) {
stack = new Stack<>();
pushToStack(root);
}
private void pushToStack(TreeNode node) {
if (node == null)
return;
while (node != null) {
stack.push(node);
node = node.left;
}
}
/**
* @return the next smallest number
*/
public int next() {
TreeNode node = stack.pop();
if (node != null) {
pushToStack(node.right);
return node.val;
}
return -1;
}
/**
* @return whether we have a next smallest number
*/
public boolean hasNext() {
return !stack.isEmpty();
}
public static void main(String[] args) {
Codec codec = new Codec();
TreeNode root;
root = codec.deserialize("[7,3,15,null,null,9,20]");
BSTIterator bstIterator = new BSTIterator(root);
while (bstIterator.hasNext()) {
System.out.println(bstIterator.next());
}
}
}
/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator obj = new BSTIterator(root);
* int param_1 = obj.next();
* boolean param_2 = obj.hasNext();
*/