-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinarySearchTreeIterator.java
More file actions
67 lines (55 loc) · 2.13 KB
/
Copy pathBinarySearchTreeIterator.java
File metadata and controls
67 lines (55 loc) · 2.13 KB
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
// Implement an iterator over a binary search tree (BST).
// Your iterator will be initialized with the root node of a BST.
// Calling next() will return the next smallest number in the BST.
// See: https://leetcode.com/problems/binary-search-tree-iterator/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.initTree;
import java.util.ArrayList;
import java.util.List;
import leetcode.util.tree.TreeNode;
public class BinarySearchTreeIterator {
/**
* This solution flattens the BST.
* The drawback of this approach is the O(N) time complexity of calling the constructor
* TODO: Add a solution with controlled recursion
*/
class BSTIterator {
private List<Integer> values = new ArrayList<>();
private int pos = 0;
public BSTIterator(TreeNode root) {
traverseTree(root);
}
private void traverseTree(TreeNode root) {
if (root != null) {
traverseTree(root.left);
values.add(root.val);
traverseTree(root.right);
}
}
/** @return the next smallest number */
public int next() {
return values.get(pos++);
}
/** @return whether we have a next smallest number */
public boolean hasNext() {
return pos < values.size();
}
}
public static void main(String[] args) {
BinarySearchTreeIterator sln = new BinarySearchTreeIterator();
TreeNode root = initTree(7, 3, 15, null, null, 9, 20);
BSTIterator iter = sln.new BSTIterator(root);
System.out.println(iter.hasNext());
System.out.println(iter.next());
System.out.println(iter.hasNext());
System.out.println(iter.next());
System.out.println(iter.hasNext());
System.out.println(iter.next());
System.out.println(iter.hasNext());
System.out.println(iter.next());
System.out.println(iter.hasNext());
System.out.println(iter.next());
System.out.println(iter.hasNext());
System.out.println(iter.hasNext());
}
}