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:
BSTIterator(TreeNode *root) {
i=0;
if(NULL==root)
{
length=0;
}
else
{
stack<TreeNode*> nodes;
TreeNode* cur=root;
while(!nodes.empty() || cur)
{
while(cur)
{
nodes.push(cur);
cur=cur->left;
}
cur=nodes.top();
nodesValue.push_back(cur->val);
nodes.pop();
cur=cur->right;
}
length=nodesValue.size();
}
} /** @return whether we have a next smallest number */
bool hasNext() {
if(i<length)
return true;
return false;
} /** @return the next smallest number */
int next() {
return nodesValue[i++];
}
private:
vector<int> nodesValue;
int i;
int length;
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/

leetCode(24):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 (InOrder traversal)

    https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...

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

    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. 【leetcode】Binary Search Tree Iterator(middle)

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

  6. 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 ...

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

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

  8. 【LeetCode】173. Binary Search Tree Iterator (2 solutions)

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

  9. LeetCode: Binary Search Tree Iterator 解题报告

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

随机推荐

  1. nodejs-安装及卸载

    linux下安装node 1.编译的方式安装​ 1 2 3 4 5 6 7 wget http://nodejs.org/dist/node-latest.tar.gz tar zxvf node-l ...

  2. MQTT---HiveMQ源代码具体解释(八)Netty-WebSocket

    源博客地址:http://blog.csdn.net/pipinet123 MQTT交流群:221405150 基于netty实现Webscoket相对来说就是相当简单,所以本讲中就不搞太复杂的了,给 ...

  3. 浅谈cocos2dx(17) 中单例管理模式

    ----我的生活,我的点点滴滴!. 首先明白一个问题.什么是管理者模式,管理类是用来管理一组相关对象的类,他提供了訪问对象的接口,假设这么说比較抽象的话.我们来看下cocos2dx中都有哪些类是管理类 ...

  4. https 证书 certbot-auto执行错误

    报错:ImportError: /root/.local/share/letsencrypt/lib/python2.7/site-packages/cryptography/hazmat/bindi ...

  5. 【scikit-learn】交叉验证及其用于參数选择、模型选择、特征选择的样例

     内容概要¶ 训练集/測试集切割用于模型验证的缺点 K折交叉验证是怎样克服之前的不足 交叉验证怎样用于选择调节參数.选择模型.选择特征 改善交叉验证 1. 模型验证回想¶ 进行模型验证的一个重要目 ...

  6. CodeForcess--609B--The Best Gift(模拟水题)

    The Best Gift Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submi ...

  7. POJ 1155 树形DP

    题意:电视台发送信号给很多用户,每个用户有愿意出的钱,电视台经过的路线都有一定费用,求电视台不损失的情况下最多给多少用户发送信号. 转自:http://www.cnblogs.com/andre050 ...

  8. caffe遇到的错误记录

    Caffe——一个多么方便的深度学习工具,为啥安装和使用起来的bug这么多呢!把痛苦的记忆记录下来,把希望留给后人. Caffe的万丈高楼(Net)是按照我们的设计图纸(prototxt),用Blob ...

  9. PHP 环境搭建工具

    PHP环境搭建工具 一键集成工具 直接安装后部署到相关目录即可浏览 phpStudy 下载地址:https://pan.baidu.com/s/1i6C3Ph7

  10. LinrFont UWP 字体预览工具下载

    Windows 10 用户 购买 https://www.microsoft.com/zh-cn/p/linrfont/9nkh5mlvt819