原题链接在这里: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. php 即使客户端或者服务器断开(如关掉浏览器)脚本也可以继续执行

    <?php ignore_user_abort(); // run script in background set_time_limit(0); // run script forever $ ...

  2. HTML中为何P标签内不可包含块元素?

    起因:在做项目时发现原本在DW中无误的代码到了MyEclipse6.0里面却提示N多错误,甚是诧异.于是究其原因,发现块级元素P内是不能嵌套DIV的. 深究:我们先来认识in-line内联元素和blo ...

  3. C++ sort vector<vector<int> > or vector<MyClass> 容器的排序

    C++的STL中提供了很强大的排序函数sort,可以对任意数组,结构体及类进行排序,下面我们先来看最简单的数组排序.默认的升序排列,我们也可以在后面加上less或greater来告诉编译器我们想要的排 ...

  4. SQL中的charindex()方法

    CHARINDEX函数返回字符或者字符串在另一个字符串中的起始位置.CHARINDEX函数调用方法如下: CHARINDEX ( expression1 , expression2 [ , start ...

  5. 记sql语句空格带来的问题

    在做分页的时候,引用了一个分页类.在一条sql语句出发生错误,没查出数据,代码如下 $sql="select * from sw_goods".$page->limit; 正 ...

  6. IOS第六天(2:10秒倒计时)

    ****************10秒倒计时 #import "HMViewController.h" @interface HMViewController () <UIA ...

  7. [转]SEP 11.x 迁移

    Sepm 迁移/灾难恢复     条件:更换服务器主机/重装服务器系统,但保持主机ip地址不更改,主机名不更改.     备份步骤:     Login 服务器控制台,进入"管理员" ...

  8. 【液晶模块系列基础视频】5.1X-GUI字体驱动1

    ============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...

  9. CSS兼容性

    1,浮动 ie6中,设置浮动的元素,width自动包裹内容了.通常我们要设定一下这个元素的宽度 子元素浮动父容器高度不能自适应的CSS解决方法 网页前端工作者经常会遇到子元素设置float浮动后导致父 ...

  10. 2016.05.04,英语,《Vocabulary Builder》Unit 22

    acerb/acri: comes from the Latin adjective acer, meaning 'sharp' or 'sour'. acerbic: [ə'sɜːrbɪk] adj ...