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: vector<int> vals;
int index;
BSTIterator(TreeNode *root) {
DFS(root);
index=-;
} void DFS(TreeNode *root)
{ if(root==NULL) return; DFS(root->left);
vals.push_back(root->val);
DFS(root->right);
} /** @return whether we have a next smallest number */
bool hasNext() { if(index<(int)vals.size()-) return true;
else return false; } /** @return the next smallest number */
int next() {
return vals[++index];
}
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/
 
 
运用二叉排序树的性质
 
首先连同root把所有左边节点都push到堆栈中,
然后每当调用next时,返回堆栈栈顶元素,然后把栈顶元素的右孩子,以及他的所有左子树都压入栈即可
 
 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
public: stack<TreeNode*> stk;
BSTIterator(TreeNode *root) {
pushLeftNode(root); } void pushLeftNode(TreeNode *root)
{ while(root!=NULL)
{
stk.push(root);
root=root->left;
}
} /** @return whether we have a next smallest number */
bool hasNext() { return !stk.empty();
} /** @return the next smallest number */
int next() { TreeNode * smallestNode=stk.top();
stk.pop();
pushLeftNode(smallestNode->right);
return smallestNode->val;
}
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/

【leetcode】Binary Search Tree Iterator的更多相关文章

  1. 【leetcode】Binary Search Tree Iterator(middle)

    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. leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)

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

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

    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. [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(24):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 (2 solutions)

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

随机推荐

  1. Java Web整合开发实战:基于Struts 2+Hibernate+Spring 目录

    第1篇 Java Web开发基础第1章 Web的工作机制( 教学视频:31分钟) 1.1 理解Web的概念 1.1.1 Web的定义 1.1.2 Web的三个核心标准 1.2 C/S与B/S两种软件体 ...

  2. Java设计模式-观察者模式(Observer)

    包括这个模式在内的接下来的四个模式,都是类和类之间的关系,不涉及到继承,学的时候应该 记得归纳,记得本文最开始的那个图.观察者模式很好理解,类似于邮件订阅和RSS订阅,当我们浏览一些博客或wiki时, ...

  3. BZOJ1045 [HAOI2008] 糖果传递

    Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数n<=987654321,表示小朋友的个数 ...

  4. Mac OS下编写对拍程序

    介绍 对拍是信息学竞赛中重要的技巧,它通过一个效率低下但正确率可以保证的程序,利用庞大的随机生成数据来验证我们的高级算法程序.对拍最大的优势在于可以通过人力所不能及的速度和数量达到验证的效果.下面我们 ...

  5. ci连贯操作的limit两个参数和sql是相反的

    ci连贯操作的limit两个参数和sql是相反的 db->where("name =",'mary')->ge() name后面要有个空格否则报错当为一个字段 -> ...

  6. iptables一些经常忘掉易混淆的参数

    -A:新增加一条规则,该规则在原规则的最后面 -p:规定应用于哪种数据包,例如:tcp,udp等 -d:(destination),表示目标IP或网络 -s:(source),表示源IP或网络 -j: ...

  7. 微信公众平台项目中遇到的小问题40016,Invalid button size

    刚辞职的同事用JAVA给客户开发的微信公众平台,今天晚上客户给我打电话说出现错误,此时我正跟朋友在外吃饭,联系已辞职的同事也联系不上,便答应回去之后我给调试看下. 问明客户说就修改了appkey和ap ...

  8. 数据库的模糊查询mybatis

    <!-- oracle --> <select id="searchUserBySearchName" parameterType="java.lang ...

  9. NoClassDefFoundError: org/hibernate/annotations/common/reflection/ReflectionManager 解决方法

    差一个jar包, 将hibernate-commons-annotations.jar加入到classpath中

  10. apache 配置多个虚拟主机

    修改文件:httd.conf 文件地址:D:\wamp\bin\apache\Apache2.2.21\conf #配置虚拟主机<VirtualHost 127.0.0.3:80>Serv ...