# 20150708 lintcode 总结 Binary Search Tree Iterator

#### Binary Search Tree Iterator

Design an iterator over a binary search tree with the following rules:

• Elements are visited in ascending order (i.e. an in-order traversal)
• next() and hasNext() queries run in O(1) time in average.Example

For the following binary search tree, in-order traversal by using iterator is [1, 6, 10, 11, 12]

   10
/    \
1      11
\       \
6       12

Challenge

Extra memory usage O(h), h is the height of the tree.

Super Star: Extra memory usage O(1)

/**
* Definition of TreeNode:
* public class TreeNode {
*     public int val;
*     public TreeNode left, right;
*     public TreeNode(int val) {
*         this.val = val;
*         this.left = this.right = null;
*     }
* }
* Example of iterate a tree:
* Solution iterator = new Solution(root);
* while (iterator.hasNext()) {
*    TreeNode node = iterator.next();
*    do something for node
* }
*/
public class Solution {
public Stack myStack = new Stack();
public TreeNode current;
//@param root: The root of binary tree.
public Solution(TreeNode root) {
// write your code here
current = root;
return;
}

//@return: True if there has next node, or false
public boolean hasNext() {
// write your code here
return (current!=null || !myStack.isEmpty());
}

//@return: return next node
public TreeNode next() {
// write your code here
while(current!=null){
myStack.push(current);
current = current.left;
}
TreeNode res = (TreeNode) myStack.pop();
current = res.right;
return res;
}
}


1. 如果current != null，一直找left.left.left.....就是所求结果；

2. 如果current == null 并且current是 current.parent的left node时，current.parent 就是所求结果，也就是myStack最上面的node；

3. 如果current == null 并且current是 current.parent的right node时， current.parent已经被pop了，current.parent.parent是所求结果，也是myStack最上面的node。

current 为结果时，再从以 current.right 为 root 的 tree 中找 next()， 所以最后要 current = current.right

• 本文已收录于以下专栏：

## Leetcode: Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro...
• doc_sgl
• 2015年01月03日 22:13
• 3630

## leetcode-173：Binary Search Tree Iterator（Java）

Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterato...
• wen1158646729
• 2015年08月18日 10:58
• 1128

## Leetcode 173 Binary Search Tree Iterator 二叉查找树迭代器

• smile_watermelon
• 2015年08月04日 18:27
• 1369

## [LeetCode 173] Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro...
• sbitswc
• 2015年08月21日 14:11
• 596

## leetcode 173: Binary Search Tree Iterator

Binary Search Tree Iterator Total Accepted: 1141 Total Submissions: 3857 Implement an iterat...
• xudli
• 2015年01月02日 12:44
• 4106

## [LeetCode]Binary Search Tree Iterator，解题报告

• zinss26914
• 2015年02月09日 21:11
• 1402

## 二叉查找树（binary search tree）——python实现

• u013184096
• 2014年07月11日 21:59
• 1515

## leetcode 98 Validate Binary Search Tree (python)

python Validate Binary Search Tree
• sxingming
• 2016年07月27日 08:36
• 690

## 173. Binary Search Tree Iterator**

mplement an iterator over a binary search tree (BST). Your iterator will be initialized with the roo...
• alwaystry
• 2016年12月24日 14:35
• 162

## [Java]leetcode173 Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro...
• fumier
• 2015年04月10日 08:52
• 633

举报原因： 您举报文章：20150708 lintcode 总结 Binary Search Tree Iterator 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)