|
6 | 6 | import java.util.Queue;
|
7 | 7 | import java.util.Stack;
|
8 | 8 |
|
9 |
| -/** |
10 |
| - * 173. Binary Search Tree Iterator |
11 |
| - * |
12 |
| - * Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. |
13 |
| - * |
14 |
| - * Calling next() will return the next smallest number in the BST. |
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. |
17 |
| - */ |
18 | 9 | public class _173 {
|
19 | 10 |
|
20 |
| - public static class Solution1 { |
| 11 | + public static class Solution1 { |
21 | 12 |
|
22 |
| - public static class BSTIterator { |
23 |
| - private Queue<Integer> queue; |
| 13 | + public static class BSTIterator { |
| 14 | + private Queue<Integer> queue; |
24 | 15 |
|
25 |
| - public BSTIterator(TreeNode root) { |
26 |
| - queue = new LinkedList<>(); |
27 |
| - if (root != null) { |
28 |
| - dfs(root, queue); |
29 |
| - } |
30 |
| - } |
| 16 | + public BSTIterator(TreeNode root) { |
| 17 | + queue = new LinkedList<>(); |
| 18 | + if (root != null) { |
| 19 | + dfs(root, queue); |
| 20 | + } |
| 21 | + } |
31 | 22 |
|
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 |
| - } |
| 23 | + private void dfs(TreeNode root, Queue<Integer> q) { |
| 24 | + if (root.left != null) { |
| 25 | + dfs(root.left, q); |
| 26 | + } |
| 27 | + q.offer(root.val); |
| 28 | + if (root.right != null) { |
| 29 | + dfs(root.right, q); |
| 30 | + } |
| 31 | + } |
41 | 32 |
|
42 |
| - public boolean hasNext() { |
43 |
| - return !queue.isEmpty(); |
44 |
| - } |
| 33 | + public boolean hasNext() { |
| 34 | + return !queue.isEmpty(); |
| 35 | + } |
45 | 36 |
|
46 |
| - public int next() { |
47 |
| - return queue.poll(); |
48 |
| - } |
| 37 | + public int next() { |
| 38 | + return queue.poll(); |
| 39 | + } |
| 40 | + } |
49 | 41 | }
|
50 |
| - } |
51 | 42 |
|
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 |
| - */ |
| 43 | + public static class Solution2 { |
| 44 | + public static class BSTIterator { |
| 45 | + /** |
| 46 | + * This is a super cool/clever idea: use a stack to store all the current left nodes of the BST, when pop(), we |
| 47 | + * push all its right nodes into the stack if there are any. |
| 48 | + * This way, we use only O(h) memory for this iterator, this is a huge saving when the tree is huge |
| 49 | + * since h could be much smaller than n. Cheers! |
| 50 | + */ |
60 | 51 |
|
61 |
| - private Stack<TreeNode> stack; |
| 52 | + private Stack<TreeNode> stack; |
62 | 53 |
|
63 |
| - public BSTIterator(TreeNode root) { |
64 |
| - stack = new Stack(); |
65 |
| - pushToStack(root, stack); |
66 |
| - } |
| 54 | + public BSTIterator(TreeNode root) { |
| 55 | + stack = new Stack(); |
| 56 | + pushToStack(root, stack); |
| 57 | + } |
67 | 58 |
|
68 |
| - private void pushToStack(TreeNode root, Stack<TreeNode> stack) { |
69 |
| - while (root != null) { |
70 |
| - stack.push(root); |
71 |
| - root = root.left; |
72 |
| - } |
73 |
| - } |
| 59 | + private void pushToStack(TreeNode root, Stack<TreeNode> stack) { |
| 60 | + while (root != null) { |
| 61 | + stack.push(root); |
| 62 | + root = root.left; |
| 63 | + } |
| 64 | + } |
74 | 65 |
|
75 |
| - public boolean hasNext() { |
76 |
| - return !stack.isEmpty(); |
77 |
| - } |
| 66 | + public boolean hasNext() { |
| 67 | + return !stack.isEmpty(); |
| 68 | + } |
78 | 69 |
|
79 |
| - public int next() { |
80 |
| - TreeNode curr = stack.pop(); |
81 |
| - pushToStack(curr.right, stack); |
82 |
| - return curr.val; |
83 |
| - } |
| 70 | + public int next() { |
| 71 | + TreeNode curr = stack.pop(); |
| 72 | + pushToStack(curr.right, stack); |
| 73 | + return curr.val; |
| 74 | + } |
| 75 | + } |
84 | 76 | }
|
85 |
| - } |
86 | 77 | }
|
0 commit comments