-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathbinarySearchTreeIterator.java
More file actions
44 lines (39 loc) · 1.32 KB
/
binarySearchTreeIterator.java
File metadata and controls
44 lines (39 loc) · 1.32 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
/*
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.
*/
/* Idea: start from the left most node, repeatly find that node's successor */
public class BSTIterator {
private TreeNode root;
private TreeNode start; /* starting node of inorder traversal */
public BSTIterator(TreeNode root) {
this.root = root;
TreeNode cur = root;
// first assign the start to the leftmost node, in case call next right away
while(cur != null && cur.left != null){
cur = cur.left;
}
start = cur;
}
/** @return whether we have a next smallest number */
public boolean hasNext() {
return start != null;
}
/** @return the next smallest number */
/* operates in o(h) time */
public int next() {
int val = start.val;
start = successor(root, start); /* get the successor of start node */
return val;
}
public TreeNode successor(TreeNode root, TreeNode p){
if(root == null) return null;
if(p.val >= root.val){
return successor(root.right, p);
}else{
TreeNode left = successor(root.left, p);
return left == null ? root : left;
}
}
}