实现一个二叉搜索树迭代器。你将使用二叉搜索树的根节点初始化迭代器。

调用 next() 将返回二叉搜索树中的下一个最小的数。

示例:

BSTIterator iterator = new BSTIterator(root);
iterator.next(); // 返回 3
iterator.next(); // 返回 7
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 9
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 15
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 20
iterator.hasNext(); // 返回 false

提示:

  • next() 和 hasNext() 操作的时间复杂度是 O(1),并使用 O(h) 内存,其中 是树的高度。
  • 你可以假设 next() 调用总是有效的,也就是说,当调用 next() 时,BST 中至少存在一个下一个最小的数。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
public:
BSTIterator(TreeNode* root) {
//初始化所有左节点入栈
for(; root != NULL; root = root->left){
stk.push(root);
}
} /** @return the next smallest number */
int next() {
TreeNode* pnode = stk.top();
stk.pop();
int val = pnode->val;
//换成右子树的根
pnode = pnode->right;
//右子树的所有左节点入栈
for(; pnode != NULL; pnode = pnode->left){
stk.push(pnode);
}
return val;
} /** @return whether we have a next smallest number */
bool hasNext() {
return stk.empty()? false: true;
}
private:
stack<TreeNode*> stk;
}; /**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator* obj = new BSTIterator(root);
* int param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/

  

leetcode_173【二叉搜索树迭代器】的更多相关文章

  1. [Swift]LeetCode173. 二叉搜索树迭代器 | Binary Search Tree Iterator

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

  2. Leetcode 173. 二叉搜索树迭代器

    题目链接 https://leetcode.com/problems/binary-search-tree-iterator/description/ 题目描述 实现一个二叉搜索树迭代器.你将使用二叉 ...

  3. 173 Binary Search Tree Iterator 二叉搜索树迭代器

    实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...

  4. Leetcode173. Binary Search Tree Iterator二叉搜索树迭代器

    实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 注意: next() 和hasNext() 操作的时间复杂度是O(1),并 ...

  5. Java实现 LeetCode 173 二叉搜索树迭代器

    173. 二叉搜索树迭代器 实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 示例: BSTIterator iterato ...

  6. 刷题-力扣-剑指 Offer II 055. 二叉搜索树迭代器

    剑指 Offer II 055. 二叉搜索树迭代器 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kTOapQ 著作权归领扣网络所有 ...

  7. LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)

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

  8. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

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

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

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

随机推荐

  1. 优化案例--改写IN条件为INNER JOIN

    --====================================== --原始语句 SET STATISTICS IO ON SELECT COUNT(DISTINCT parent_co ...

  2. Transaction And Lock--锁相关基础

    --=======================================================--锁提示--holdlock :将共享锁保留到事务完成,而不是在相应的表.行或数据页 ...

  3. Nutch2.2.1 爬虫问题列表

    http://www.cnblogs.com/cy163/archive/2013/02/14/2912630.html http://blog.csdn.net/wangzhaodong001/ar ...

  4. Python数据模型建立

    基本结构AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 pri ...

  5. jquery源码解析:jQuery扩展方法extend的详解

    jQuery中要扩展方法或者属性都是通过extend方法实现的.所谓的jQuery插件也是通过extend方法实现的. jQuery.extend扩展的是工具方法,也就是静态方法.jQuery.fn. ...

  6. shell的算术运算

    变量的数值计算方法大致有双括号 (()), expr,  bc, $[ ] 例子1 注意:2**3表示2的3次方,a++表示先输出a自身的值,然后进行++的运算: --a表示先进行--的运算,然后再输 ...

  7. Python 标准库之 xml.etree.ElementTree

    Python 标准库之 xml.etree.ElementTree Python中有多种xml处理API,常用的有xml.dom.*模块.xml.sax.*模块.xml.parser.expat模块和 ...

  8. software installing

    <1>.Apache防火墙配置 firewall-cmd --add-service=http firewall-cmd --add-service=https 防火墙通过80和443端口 ...

  9. 使用bluebird解决promise兼容性问题

    //引入promiseif(!Promise){ var Promise = require("bluebird"); // Configure Promise.config({ ...

  10. 为什么sudo执行命令还是会提示权限不够

    安装openstack 的过程中需要执行以下命令,虽然使用了sudo,但是依然提示权限不够 $ sudo echo "deb http://ubuntu-cloud.archive.cano ...