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 {
vector<int> v;
int pos;
public:
BSTIterator(TreeNode *root) {
pos = ;
stack<TreeNode*> s;
TreeNode *p = root, *pre = NULL;
while(p || !s.empty())
{
while(p)
{
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
s.pop();
v.push_back(p->val);
p = p->right;
}
}
} /** @return whether we have a next smallest number */
bool hasNext() {
return pos < v.size();
} /** @return the next smallest number */
int next() {
return v[pos++];
}
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/

173. Binary Search Tree Iterator -- 迭代器的更多相关文章

  1. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

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

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

  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 173. Binary Search Tree Iterator

    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. 173. Binary Search Tree Iterator

    题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with th ...

  8. leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)

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

  9. LC 173. Binary Search Tree Iterator

    题目描述 Implement an iterator over a binary search tree (BST). Your iterator will be initialized with t ...

随机推荐

  1. Django - 模型层 - 下

    一.多表 sql 单表 多表 多对一 多对多 一对一 =============================================== 一对多:Book id title price p ...

  2. Django中URL的解析和反查

    add by zhj: 如果想用reverse(namespace1:namespace2:...:namespaceN:name)反查url(注意:用reverse('polls:index')方法 ...

  3. 【Python】获取翻页之后的各页面中的属性值。

    如何获取翻页之后的页面中的html标签中的属性值? # coding=utf-8 from selenium import webdriver if __name__=="__main__& ...

  4. mysql 数据操作 单表查询 group by 介绍

    group by 是在where 之后运行 在写单表查询语法的时候 应该把group by 写在 where 之后 执行顺序 1.先找到表 from 库.表名 2.按照where 约束条件 过滤你想要 ...

  5. MegaCli 监控raid状态

    MegaCli 监控raid状态 http://blog.chinaunix.net/uid-25135004-id-3139293.html 简介 MegaCli是一款管理维护硬件RAID软件,可以 ...

  6. [WorldWind学习]23.TerrainAccessor

    QuadTile的CreateElevatedMesh()方法中: //获取地形瓦片 TerrainTile tile = QuadTileSet.World.TerrainAccessor.GetE ...

  7. Centos7使用yum下载rpm包而不执行安装

    yum-downloadonly是yum的一个插件,使得yum可以从RHN或者yum的仓库只下载包而不安装. 举例,从yum源下载ricci软件包 # yum install --downloadon ...

  8. soapUI-DataSource

    1.1.1.1 概述 - 数据源   Option Description   Properties DataSource属性表   Toolbar DataSource工具栏   Configura ...

  9. [华为]查找两个字符串a,b中的最长公共子

    链接:https://www.nowcoder.com/questionTerminal/181a1a71c7574266ad07f9739f791506来源:牛客网 查找两个字符串a,b中的最长公共 ...

  10. gerrit上sshkey设置问题

    gerrit里面设置ssh的方法  http://blog.sina.com.cn/s/blog_4d4bc1110101dbxs.html `ssh-keygen -t dsa -b 1024` d ...