链接:https://leetcode-cn.com/problems/binary-search-tree-iterator/
class BSTIterator {
LinkedList<TreeNode> stack=new LinkedList<>();
public BSTIterator(TreeNode root) {
TreeNode cur=root;
while(cur!=null){
stack.push(cur);
cur=cur.left;
}
}
public int next() {
TreeNode n=stack.pop();
TreeNode cur=n.right;
while(cur!=null){
stack.push(cur);
cur=cur.left;
}
return n.val;
}
public boolean hasNext() {
return !stack.isEmpty();
}
}