|
8 | 8 |
|
9 | 9 | /** |
10 | 10 | * 173. Binary Search Tree Iterator |
| 11 | + * |
11 | 12 | * Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. |
12 | | - * <p> |
| 13 | + * |
13 | 14 | * Calling next() will return the next smallest number in the BST. |
14 | | - * <p> |
| 15 | + * |
15 | 16 | * Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree. |
16 | 17 | */ |
17 | 18 | public class _173 { |
18 | 19 |
|
19 | | - public static class Solution1 { |
| 20 | + public static class Solution1 { |
20 | 21 |
|
21 | | - public static class BSTIterator { |
| 22 | + public static class BSTIterator { |
| 23 | + private Queue<Integer> queue; |
22 | 24 |
|
23 | | - private Queue<Integer> queue; |
24 | | - |
25 | | - /** |
26 | | - * My natural idea is to use a queue to hold all elements in the BST, traverse it while constructing the iterator, although |
27 | | - * this guarantees O(1) hasNext() and next() time, but it uses O(n) memory. |
28 | | - */ |
29 | | - //Cheers! Made it AC'ed at first shot! Praise the Lord! Practice does make perfect! |
30 | | - //I created a new class to do it using Stack to meet O(h) memory: {@link fishercoder.algorithms._173_using_stack} |
31 | | - public BSTIterator(TreeNode root) { |
32 | | - queue = new LinkedList<>(); |
33 | | - if (root != null) { |
34 | | - dfs(root, queue); |
35 | | - } |
36 | | - } |
37 | | - |
38 | | - private void dfs(TreeNode root, Queue<Integer> q) { |
39 | | - if (root.left != null) { |
40 | | - dfs(root.left, q); |
41 | | - } |
42 | | - q.offer(root.val); |
43 | | - if (root.right != null) { |
44 | | - dfs(root.right, q); |
45 | | - } |
46 | | - } |
47 | | - |
48 | | - /** |
49 | | - * @return whether we have a next smallest number |
50 | | - */ |
51 | | - public boolean hasNext() { |
52 | | - return !queue.isEmpty(); |
53 | | - } |
54 | | - |
55 | | - /** |
56 | | - * @return the next smallest number |
57 | | - */ |
58 | | - public int next() { |
59 | | - return queue.poll(); |
60 | | - } |
| 25 | + public BSTIterator(TreeNode root) { |
| 26 | + queue = new LinkedList<>(); |
| 27 | + if (root != null) { |
| 28 | + dfs(root, queue); |
61 | 29 | } |
62 | | - } |
63 | | - |
64 | | - public static class Solution2 { |
65 | | - public static class BSTIterator { |
66 | | - /** |
67 | | - * This is a super cool/clever idea: use a stack to store all the current left nodes of the BST, when pop(), we |
68 | | - * push all its right nodes into the stack if there are any. |
69 | | - * This way, we use only O(h) memory for this iterator, this is a huge saving when the tree is huge |
70 | | - * since h could be much smaller than n. Cheers! |
71 | | - */ |
| 30 | + } |
72 | 31 |
|
73 | | - private Stack<TreeNode> stack; |
| 32 | + private void dfs(TreeNode root, Queue<Integer> q) { |
| 33 | + if (root.left != null) { |
| 34 | + dfs(root.left, q); |
| 35 | + } |
| 36 | + q.offer(root.val); |
| 37 | + if (root.right != null) { |
| 38 | + dfs(root.right, q); |
| 39 | + } |
| 40 | + } |
74 | 41 |
|
75 | | - public BSTIterator(TreeNode root) { |
76 | | - stack = new Stack(); |
77 | | - pushToStack(root, stack); |
78 | | - } |
| 42 | + public boolean hasNext() { |
| 43 | + return !queue.isEmpty(); |
| 44 | + } |
79 | 45 |
|
80 | | - private void pushToStack(TreeNode root, Stack<TreeNode> stack) { |
81 | | - while (root != null) { |
82 | | - stack.push(root); |
83 | | - root = root.left; |
84 | | - } |
85 | | - } |
| 46 | + public int next() { |
| 47 | + return queue.poll(); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static class Solution2 { |
| 53 | + public static class BSTIterator { |
| 54 | + /** |
| 55 | + * This is a super cool/clever idea: use a stack to store all the current left nodes of the BST, when pop(), we |
| 56 | + * push all its right nodes into the stack if there are any. |
| 57 | + * This way, we use only O(h) memory for this iterator, this is a huge saving when the tree is huge |
| 58 | + * since h could be much smaller than n. Cheers! |
| 59 | + */ |
| 60 | + |
| 61 | + private Stack<TreeNode> stack; |
| 62 | + |
| 63 | + public BSTIterator(TreeNode root) { |
| 64 | + stack = new Stack(); |
| 65 | + pushToStack(root, stack); |
| 66 | + } |
| 67 | + |
| 68 | + private void pushToStack(TreeNode root, Stack<TreeNode> stack) { |
| 69 | + while (root != null) { |
| 70 | + stack.push(root); |
| 71 | + root = root.left; |
| 72 | + } |
| 73 | + } |
86 | 74 |
|
87 | | - public boolean hasNext() { |
88 | | - return !stack.isEmpty(); |
89 | | - } |
| 75 | + public boolean hasNext() { |
| 76 | + return !stack.isEmpty(); |
| 77 | + } |
90 | 78 |
|
91 | | - public int next() { |
92 | | - TreeNode curr = stack.pop(); |
93 | | - pushToStack(curr.right, stack); |
94 | | - return curr.val; |
95 | | - } |
96 | | - } |
| 79 | + public int next() { |
| 80 | + TreeNode curr = stack.pop(); |
| 81 | + pushToStack(curr.right, stack); |
| 82 | + return curr.val; |
| 83 | + } |
97 | 84 | } |
| 85 | + } |
98 | 86 | } |
0 commit comments