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.

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
public: queue<int> Q;
public:
BSTIterator(TreeNode *root) {
inOrder(root, Q);
} void inOrder(TreeNode* root, queue<int>& Q) {
if(root != NULL) {
inOrder(root->left, Q);
Q.push(root->val);
inOrder(root->right, Q);
}
} /** @return whether we have a next smallest number */
bool hasNext() {
return !Q.empty();
} /** @return the next smallest number */
int next() {
int next_min = Q.front();
Q.pop();
return next_min;
}
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/

leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)的更多相关文章

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

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

  2. leetcode 173. Binary Search Tree Iterator

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

  3. Java for LeetCode 173 Binary Search Tree Iterator

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

  4. [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器

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

  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】173. Binary Search Tree Iterator (2 solutions)

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

  7. 【leetcode】Binary Search Tree Iterator

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

  8. 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...

  9. [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

随机推荐

  1. PHP程序员函数注释规格

    <?php/*** @name 名字* @abstract 申明变量/类/方法* @access 指明这个变量.类.函数/方法的存取权限* @author 函数作者的名字和邮箱地址* @cate ...

  2. Samza在YARN上的启动过程 =》 之一

    运行脚本,提交job 往YARN提交Samza job要使用run-job.sh这个脚本. samza-example/target/bin/run-job.sh  --config-factory= ...

  3. VS2005中SetUnhandledExceptionFilter函数应用

    很多软件通过设置自己的异常捕获函数,捕获未处理的异常,生成报告或者日志(例如生成mini-dump文件),达到Release版本下追踪Bug的目的.但是,到了VS2005(即VC8),Microsof ...

  4. GIT:本地有更改,但强制作远程仓库里作更新

    有时,紧急线上修改时,这个功能有用处的. git fetch --all git reset --hard origin/master ================ git reset --har ...

  5. java使用redis

    1.redis和memecache这种缓存框架,都分为服务器端和客户端. 我们在项目中使用,相当于客户端,要引入客户端使用redis的jar包 首先你需要下载驱动包,下载 jedis.jar,确保下载 ...

  6. 最短路径算法之三——Bellman-Ford算法

    Bellman-Ford算法 Dijkstra算法无法判断含负权边的图的最短路. 如果遇到负权,在没有负权回路存在时,即便有负权的边,也可以采用Bellman-Ford算法正确求出最短路径. PS:负 ...

  7. 【HDOJ】4089 Activation

    1. 题目描述长度为n的等待队列,tomato处于第m个,有如下四种可能:(1)激活失败,概率为$p_1$,队列中的顺序不变:(2)连接失败,概率为$p_2$,队头玩家重新排在队尾:(3)激活成功,概 ...

  8. Android开发之View动画效果插补器Interpolator

    插补器Interpolator 官网描述:An interpolator defines the rate of change of an animation. This allows the bas ...

  9. mysql系列命令解释

    mysqld - the MySQL server mysql - the MySQL command-line tool mysqlaccess - client for checking acce ...

  10. struts采用JavaServlet/JSP技术,实现了基于Java EEWeb应用的MVC设计模式的应用框架

    今天我用Ecipse搭建Struts框架,并且使用Struts框架编写一个最简单的例子,相信读者能够很容易的明白. Struts是当今Java比较流行的三大框架之一,三大框架是Struts,sprin ...