leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
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)的更多相关文章
- ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- leetcode 173. Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 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 ...
- [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 【LeetCode】173. Binary Search Tree Iterator (2 solutions)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 【leetcode】Binary Search Tree Iterator
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...
- [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 ...
随机推荐
- JUnit 4 使用 Java 5 中的注解(annotation)
JUnit 4 使用 Java 5 中的注解(annotation),以下是JUnit 4 常用的几个 annotation 介绍@Before:初始化方法@After:释放资源@Test:测试方法, ...
- ural 1200
推出公式 然后特判两端 代码其实挺烂 但是有人竟然可以直接暴过去的 ...... #include <cstdio> #include <cstring> #in ...
- spring中注解事务认识
1.配置事务管理器 <!-- 设定transactionManager事务管理器 --> <bean id="txManager" class="org ...
- ECNU-2574 Principles of Compiler
题意: 给出编译规则,求是否满足条件 A:= '(' B')'|'x'. B:=AC. C:={'+'A}. 其中{}表示里面的内容可以出现0次或者多次 注意点见代码注释 #include ...
- cocos2dx addchild坐标问题
a.addchild(b); 会把a->getBoundingBox矩形的左下角坐标点和b的锚点贴合在一起. ----- 其他引擎默认不是这样的,所以再跨平台导数据的时候,要注意这些细微的差别 ...
- Qt4.6.2已编译二进制版本在VS2005中的问题
结论1:如果你想把Qt4.6.2安装在VS2005中,又不想花时间编译,请下载和安装qt-win-opensource-4.6.2-vs2008,并单独编译“QT安装路径/src/winmain/” ...
- php Ajax 局部刷新
php Ajax 局部刷新: HTML部分 </head> <body> <h1>Ajax动态显示时间</h1> <input type=&quo ...
- nigix以及相关
nginx+php的配置 php与nginx整合 http://www.thinkphp.cn/topic/13082.html [入门篇]Nginx + FastCGI 程序(C/C++) 搭建高性 ...
- Android开发之ContentValues
SQLite数据库进行CRUD的时候, 用到了ContentValues类,负责存储名值对,名都是String类型,值都是基本类型. 例子: ContentValues values=new Cont ...
- SQL列数据转换为字符串
行列转换,将列数据转换为字符串输出 ) SET @center_JZHW = ( SELECT DISTINCT STUFF( ( SELECT ',' + ce_code FROM ap_cente ...