原题链接在这里:https://leetcode.com/problems/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.

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.

题解:

用一个stack把所有最左边的node压进 stack中。

判断hasNext()时就是看stack 是否为空.

next()返回stack顶部top元素,若是top有右子树,就把右子树的所有最左node压入stack中.

constructor time complexity: O(h).

hashNext time complexity: O(1).

next time complexity: O(h).

Space: O(h), stack 最大为树的高度。

AC Java:

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

类似Binary Tree Inorder TraversalInorder Successor in BST.

LeetCode 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] Binary Search Tree Iterator 二叉搜索树迭代器

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

  3. LeetCode——Binary Search Tree Iterator

    Description: Implement an iterator over a binary search tree (BST). Your iterator will be initialize ...

  4. [LeetCode] Binary Search Tree Iterator 深度搜索

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

  5. leetcode Binary Search Tree Iterator python

    # Definition for a binary tree node # class TreeNode(object): # def __init__(self, x): # self.val = ...

  6. 【leetcode】Binary Search Tree Iterator

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

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

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

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

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

  9. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. 低调的华丽,Windows Server 2003 ... 写给厌倦了XP,但又纠结于vista/win7花哨的童鞋(转)

    发布于2001年10月25日的windows XP 距今已近8年 时间, 微软从没有一个操作系统能像XP那样  坚挺这么久,婚姻既有7年之痒,何况用了8年XP的广大 同学,但07年发布的vista似乎 ...

  2. android开发 BaseAdapter中getView()里的3个参数是什么意思

    BaseAdapter适配器里有个getView()需要重写public View getView(int position,View converView,ViewGroup parent){ // ...

  3. CentOS mysql硬盘满了挂载阿里云硬盘

    前提,昨天晚上导入数据库到本地时候发现硬盘满了,出了,好多错,这边在目录下新建了一个/mysql这样的数据库目录,再将/etc/my.cnf 下的datadir 指向到/mysql下,就可以了 阿里云 ...

  4. ci调用application/views下的css,js,图片资源出现You don't have permission to access CodeIgniter on this server解决

    原因是view文件下面有个.htaccess文件,里面写的是 Deny from all     //拒绝所有请求 自己本地测试的话,就直接去掉,放到服务器就指定application/views文件 ...

  5. bash: /usr/lib/jvm/jdk1.7.0_80/bin/java: No such file or directory 问题

    在安装java的时候,经常会遇到一些奇奇怪怪的问题. 在配置好环境变量之后,执行java -version,出现: bash: /usr/lib/jvm/jdk1.7.0_80/bin/java: N ...

  6. 《你不知道的JavaScript》读书笔记(二)词法作用域

    JavaScript 采用的是 词法作用域 的工作模型. 定义 词法化:大部分标准语言编译器的第一个工作阶段叫词法化(单词化),这个过程会对源代码中的字符进行检查,如果是有状态的解析过程,还会赋予单词 ...

  7. java--接口和抽象类

    接口将抽象类的概念更延伸了一步,完全禁止了所有的函数定义.且可以将多个接口合并到一起,但是不能继承多个类.

  8. pureftpd安装配置[总结]

    http://www.ttlsa.com/linux/how-to-install-pureftpd/ 看了这篇文章[几个小坑]总结如下: 1.最重要的一点,代码不要复制,有些符号肉眼看不出来. ./ ...

  9. Andrew Ng机器学习公开课笔记–Reinforcement Learning and Control

    网易公开课,第16课 notes,12 前面的supervised learning,对于一个指定的x可以明确告诉你,正确的y是什么 但某些sequential decision making问题,比 ...

  10. hibernate多SessionFactory配置

    <bean id="aDataSource" class="org.apache.commons.dbcp.BasicDataSource" destro ...