Question

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.

Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.

Solution

When a problem relates to BST and sequence, we should think about in-order traversal of BST.

We find that the original codes of Binary Tree Inorder Traversal can be modified to fit following codes.

while (it.hasNext()) {
System.out.println(it.next());
}

Note that before print, we need to push nodes to stack first.

Inorder traversal time complexity is O(n), so for each next() step, average time is O(1).

And the stack costs O(h) because we either goes down or pop a node and then goes down.

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator {
private Stack<TreeNode> stack;
private TreeNode current; public BSTIterator(TreeNode root) {
stack = new Stack<TreeNode>();
current = root;
while (current != null) {
stack.push(current);
current = current.left;
} } /** @return whether we have a next smallest number */
public boolean hasNext() {
return (! stack.empty() || current != null);
} /** @return the next smallest number */
public int next() {
while (current != null) {
stack.push(current);
current = current.left;
}
TreeNode tmp = stack.pop();
int result = tmp.val;
current = tmp.right;
return result;
}
} /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/

Binary Search Tree Iterator 解答的更多相关文章

  1. 【leetcode】Binary Search Tree Iterator

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  2. leetcode-173:Binary Search Tree Iterator(Java)

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  3. 【LeetCode】173. Binary Search Tree Iterator (2 solutions)

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  4. LeetCode: Binary Search Tree Iterator 解题报告

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  5. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  6. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  7. ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  8. leetcode 173. Binary Search Tree Iterator

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. 【leetcode】Binary Search Tree Iterator(middle)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. Best Meeting Point 解答

    Question A group of two or more people wants to meet and minimize the total travel distance. You are ...

  2. 2013成都网赛 G(x) (HDU 4733)

    G(x) 思路: 首先搞清楚每个位置上的值有什么意义, 如果第i位的值为1则 第i位与第i+1位不同,反之相同. 然后考虑s1和s2为什么会不一样, 这是由于x+1后比特位进位导致的,于是得出一个性质 ...

  3. DBA 经典面试题(3)

    这里的回答并不是十分全面,这些问题可以通过多个角度来进行解释,也许你不必在面试过程中给出完全详尽的答案,只需要通过你的解答使面试考官了解你对ORACLE概念的熟悉程度.   1.解释冷备份和热备份的不 ...

  4. dig命令(转载)

    dig命令使用大全(linux上域名查询) 可以这样说,翻译本篇文档的过程就是我重新学习DNS的过程,dig命令可以帮助我们学习DNS的原理,配置,以及其查询过程.以前使用dig仅仅是查询一下A记录或 ...

  5. .NET 面试题(1)

    1.简述 private. protected. public. internal 修饰符的访问权限. private:私有成员,在类的内部才能访问 protected:保护成员,在该类内部和继承本类 ...

  6. 炉石传说__multiset

     炉石传说  Problem Description GG学长虽然并不打炉石传说,但是由于题面需要他便学会了打炉石传说.但是传统的炉石传说对于刚入门的GG学长来说有点复杂,所以他决定自己开发一个简化版 ...

  7. 大到可以小说的Y组合子(三)

    答:关于Fix的问题你fix了吗? 问:慢着,让我想想,上次留下个什么问题来着?是说我们有了一个求不动点的函数Fix,但Fix却是显式递归的,是吧? 答:有劳你还记的这个问题. 问:Fix的参与背离了 ...

  8. Linux 下提高make的编译效率

    Linux下安装程序,一般都通过包管理器安装,但是包管理器或软件商店里的软件往往不是最新版本的,安装最新版软件时通常是下载源代码进行编译. 编译安装源代码时就离不开make了,但是make是单线程的, ...

  9. easyui-combobox绑定json数据

    用的C#语言 后台取数据,就不用废话了,先看看序列化json数据 /// <summary> /// 对象转JSON /// </summary> /// <param ...

  10. jquery.validate的使用

    在页面上面引用 <script type="text/JavaScript" src="js/jQuery.js"></script> ...