Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Callingnext()will return the next smallest number in the BST.
Note:next()andhasNext()should run in average O(1) time and uses O(h) memory, wherehis the height of the tree.
分析
就是inorder的while遍历拆成2部分。
public class BSTIterator {
private TreeNode next;
private Stack<TreeNode> s = new Stack<TreeNode>();
private void addToStack(){
while(next != null){
s.add(next);
next = next.left;
}
next = null;
}
public BSTIterator(TreeNode root) {
next = root;
}
/** @return whether we have a next smallest number */
public boolean hasNext() {
if(next != null){
addToStack();
}
return !s.isEmpty();
}
/** @return the next smallest number */
public int next() {
if(!hasNext()){
return -1;
}
next = s.pop();
int ret = next.val;
next = next.right;
return ret;
}
}不用额外函数的做法
二刷错误:1.忘了用栈 2. hasNext里的while写错了,root不空的话,先推入栈再转左。
Last updated
Was this helpful?