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.

采用中序遍历将节点压入栈中,由于要求存储空间为O(h),因此不能在一开始将所有节点全部压入,只是压入最左边一列。当取出一个节点时,压入其右子节点的所有左节点。

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
private:
stack <TreeNode*> stk;
int minres;
public:
BSTIterator(TreeNode *root) {
while(root)
{
stk.push(root);
root=root->left;
}
} /** @return whether we have a next smallest number */
bool hasNext() {
if(stk.empty())
return false;
TreeNode* top;
top=stk.top();
minres=top->val;
stk.pop();
TreeNode* cur=top->right;
if(cur)
{
stk.push(cur);
cur=cur->left;
while(cur)
{
stk.push(cur);
cur=cur->left;
}
}
return true;
} /** @return the next smallest number */
int next() {
return minres;
}
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << 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. 错误:升级为xcode8之后无法上网的解决方法

    主要是在info.list中增加以下的节点,因为XCode开始所有的http都转为https来联网了. 添加类型为Dictionary的:NSAppTransportSecurity 再添加节点为:N ...

  2. Matlab2014下载和破解方法,以及Matlab很好的学习网站

    ISO镜像下载地址链接: http://pan.baidu.com/s/1i31bu5J 密码: obo1  单独破解文件下载链接: http://pan.baidu.com/s/1c0CGQsw 密 ...

  3. CFString​Transform

    Mattt Thompson撰写. Ricky Tan翻译. 发布于2012年8月6日关于一种语言好不好用,你只需要衡量以下两种指标: API 的统一性String 类的实现质量NSString 是基 ...

  4. 基于Ubuntu虚拟机安装edx-platform

    基于Ubuntu虚拟机安装edx-platform   一. 前提准备 1. 虚拟机中安装Ubuntu12.04,然后再使用Vagrant方式搭建开发环境,请确保这个虚拟机可以使用2GB的内存,否则容 ...

  5. Oracle 数据库二 基本查询

    查询当前用户:show user 查看当前用户下的表:select *from tab; 设置行宽: show linesize;(查看行宽)     set linesize 120;(设置行宽) ...

  6. 人生导师——如何学习C++的Windows方向

    原来发在百度上,今天被人挖坟挖出来了,我就再贴出来吧. -----------------------有什么说的不对的地方---------------------- 本文原创,转载请注明出处并保持文 ...

  7. Oracle 11g 中恢复管理器RMAN介绍

    这是我平时摘录的笔记,从管理艺术那本书上摘录出来的,放到这里 RMAN 可在数据库服务器的帮助下从数据库内备份数据文件,可构造数据文件映像副本.控制文件和控制文件映像.对当日志 SPFILE 和RMA ...

  8. maven 仓库搜索添加需要的jar包

    可用仓库网址: http://search.maven.org/#browse http://mvnrepository.com/ http://repository.sonatype.org/ind ...

  9. [部署]CentOS安装apache

    环境 虚拟机:VMWare10.0.1 build-1379776 操作系统:CentOS7 64位 步骤 1.使用yum安装 yum install httpd httpd-devel 2.启动 a ...

  10. Eclipse中修改SVN用户名和密码方法(转)

    由于在svn 的界面中并没有为我们提供直接更换用户名密码的地方,所以一旦我们需要更换用户名的就需要自己想一些办法. 解决方案: 在Eclipse 使用SVN 的过程中大多数人往往习惯把访问SVN 的用 ...