|
6 | 6 | importjava.util.Queue; |
7 | 7 | importjava.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 | publicclass_173 { |
19 | 10 |
|
20 | | -publicstaticclassSolution1 { |
| 11 | +publicstaticclassSolution1 { |
21 | 12 |
|
22 | | -publicstaticclassBSTIterator { |
23 | | -privateQueue<Integer>queue; |
| 13 | +publicstaticclassBSTIterator { |
| 14 | +privateQueue<Integer>queue; |
24 | 15 |
|
25 | | -publicBSTIterator(TreeNoderoot) { |
26 | | -queue =newLinkedList<>(); |
27 | | -if (root !=null) { |
28 | | -dfs(root,queue); |
29 | | - } |
30 | | - } |
| 16 | +publicBSTIterator(TreeNoderoot) { |
| 17 | +queue =newLinkedList<>(); |
| 18 | +if (root !=null) { |
| 19 | +dfs(root,queue); |
| 20 | +} |
| 21 | +} |
31 | 22 |
|
32 | | -privatevoiddfs(TreeNoderoot,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 | +privatevoiddfs(TreeNoderoot,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 | | -publicbooleanhasNext() { |
43 | | -return !queue.isEmpty(); |
44 | | - } |
| 33 | +publicbooleanhasNext() { |
| 34 | +return !queue.isEmpty(); |
| 35 | +} |
45 | 36 |
|
46 | | -publicintnext() { |
47 | | -returnqueue.poll(); |
48 | | - } |
| 37 | +publicintnext() { |
| 38 | +returnqueue.poll(); |
| 39 | + } |
| 40 | + } |
49 | 41 | } |
50 | | - } |
51 | 42 |
|
52 | | -publicstaticclassSolution2 { |
53 | | -publicstaticclassBSTIterator { |
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 | +publicstaticclassSolution2 { |
| 44 | +publicstaticclassBSTIterator { |
| 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 | | -privateStack<TreeNode>stack; |
| 52 | +privateStack<TreeNode>stack; |
62 | 53 |
|
63 | | -publicBSTIterator(TreeNoderoot) { |
64 | | -stack =newStack(); |
65 | | -pushToStack(root,stack); |
66 | | - } |
| 54 | +publicBSTIterator(TreeNoderoot) { |
| 55 | +stack =newStack(); |
| 56 | +pushToStack(root,stack); |
| 57 | +} |
67 | 58 |
|
68 | | -privatevoidpushToStack(TreeNoderoot,Stack<TreeNode>stack) { |
69 | | -while (root !=null) { |
70 | | -stack.push(root); |
71 | | -root =root.left; |
72 | | - } |
73 | | - } |
| 59 | +privatevoidpushToStack(TreeNoderoot,Stack<TreeNode>stack) { |
| 60 | +while (root !=null) { |
| 61 | +stack.push(root); |
| 62 | +root =root.left; |
| 63 | +} |
| 64 | +} |
74 | 65 |
|
75 | | -publicbooleanhasNext() { |
76 | | -return !stack.isEmpty(); |
77 | | - } |
| 66 | +publicbooleanhasNext() { |
| 67 | +return !stack.isEmpty(); |
| 68 | +} |
78 | 69 |
|
79 | | -publicintnext() { |
80 | | -TreeNodecurr =stack.pop(); |
81 | | -pushToStack(curr.right,stack); |
82 | | -returncurr.val; |
83 | | - } |
| 70 | +publicintnext() { |
| 71 | +TreeNodecurr =stack.pop(); |
| 72 | +pushToStack(curr.right,stack); |
| 73 | +returncurr.val; |
| 74 | + } |
| 75 | + } |
84 | 76 | } |
85 | | - } |
86 | 77 | } |