Skip to content

Commit b00d05c

Browse files
BST iterator
1 parent 832d877 commit b00d05c

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

Common/src/utils/CommonUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,13 @@ public static void print(List<String> list) {
140140
System.out.println();
141141
}
142142

143+
public static void printIntegerList(List<List<Integer>> res) {
144+
for(List<Integer> list : res){
145+
for(int i : list){
146+
System.out.print(i + ", ");
147+
}
148+
System.out.println();
149+
}
150+
}
151+
143152
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package medium;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
import classes.TreeNode;
7+
8+
/**173. Binary Search Tree Iterator QuestionEditorial Solution My Submissions
9+
Total Accepted: 56053
10+
Total Submissions: 154876
11+
Difficulty: Medium
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+
public class BSTIterator_using_q {
18+
19+
private Queue<Integer> q;
20+
21+
/**My natural idea is to use a queue to hold all elements in the BST, traverse it while constructing the iterator, although
22+
* this guarantees O(1) hasNext() and next() time, but it uses O(n) memory.*/
23+
//Cheers! Made it AC'ed at first shot! Praise the Lord! Practice does make perfect!
24+
//I created a new class to do it using Stack to meet O(h) memory: {@link medium.BSTIterator_using_stack}
25+
public BSTIterator_using_q(TreeNode root) {
26+
q = new LinkedList<Integer>();
27+
if(root != null) dfs(root, q);
28+
}
29+
30+
private void dfs(TreeNode root, Queue<Integer> q) {
31+
if(root.left != null) dfs(root.left, q);
32+
q.offer(root.val);
33+
if(root.right != null) dfs(root.right, q);
34+
}
35+
36+
/** @return whether we have a next smallest number */
37+
public boolean hasNext() {
38+
return !q.isEmpty();
39+
}
40+
41+
/** @return the next smallest number */
42+
public int next() {
43+
return q.poll();
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package medium;
2+
3+
import java.util.Stack;
4+
5+
import classes.TreeNode;
6+
7+
/**173. Binary Search Tree Iterator QuestionEditorial Solution My Submissions
8+
Total Accepted: 56053
9+
Total Submissions: 154876
10+
Difficulty: Medium
11+
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
12+
13+
Calling next() will return the next smallest number in the BST.
14+
15+
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+
public class BSTIterator_using_stack {
17+
/**This is a super cool/clever idea: use a stack to store all the current left nodes of the BST, when pop(), we
18+
* push all its right nodes into the stack if there are any.
19+
* This way, we use only O(h) memory for this iterator, this is a huge saving when the tree is huge
20+
* since h could be much smaller than n. Cheers!*/
21+
22+
private Stack<TreeNode> stack;
23+
24+
public BSTIterator_using_stack(TreeNode root) {
25+
stack = new Stack();
26+
pushToStack(root, stack);
27+
}
28+
29+
private void pushToStack(TreeNode root, Stack<TreeNode> stack) {
30+
while(root != null){
31+
stack.push(root);
32+
root = root.left;
33+
}
34+
}
35+
36+
public boolean hasNext() {
37+
return !stack.isEmpty();
38+
}
39+
40+
public int next() {
41+
TreeNode curr = stack.pop();
42+
pushToStack(curr.right, stack);
43+
return curr.val;
44+
}
45+
}

0 commit comments

Comments
 (0)