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. 用SQL打印乘法口诀表

    --用SQL打印出乘法口诀表 declare @i int ,@j int --@i是乘法口诀的行数 --一共九行 begin --每次都是从1*开始,j每循环一次递增 )--print每次输出都会换 ...

  2. Eclipse-插件的安装之link文件方法

    1. 我的eclipse路径为eclipse_Home,在eclipse文件夹下建文件夹MyPlugins. 2. 下载插件并解压得到包含features和plugins的文件夹theXXX. 3.  ...

  3. yield实例

    如下 # __author__ = liukun # coding:utf-8 def it(): print ('hello') yield 1 yield 1 a= it() print(&quo ...

  4. 【poj1745】 Divisibility

    http://poj.org/problem?id=1745 (题目链接) 题意 给出n串数,可以在其两两之间添加+或-,判断是否存在某种方案使得出的表达式的答案可以整除k. Solution 水题一 ...

  5. RedGate .NET Reflector注册问题(反注册)

    Reflector分为桌面版和VS集成版本,当我们使用注册机注册的时候如果注册了Standvard版本,那么我们的VS就不能集成查看,也不能Debug,那么这 显然不是我们想要的,我们会选择重新注册, ...

  6. Linux系统如何查看版本信息

    输入"uname -a ",可显示电脑以及操作系统的相关信息.   输入"cat /proc/version",说明正在运行的内核版本.   输入"c ...

  7. eclipse下环境变量设置:eclipse导入工程出现 Unbound classpath variable Error

    在导入网友提供的Tomcat源码工程时候出现了 The project cannot be build until build path errors are resolved Unbound cla ...

  8. java 内存分析

    Java堆内存(heap memory)的十个要点: 1. Java堆内存是操作系统分配给JVM的内存的一部分. 2. 当我们创建对象时,它们存储在Java堆内存中. 3. 为了便于垃圾回收,Java ...

  9. LWP 轻量级线程的意义与实现

    转子 http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 二.Linux 2.4内核中的轻量进程实现 最初的进程定义都包含程序.资源 ...

  10. JavaScript 技巧总结

    日期1. 日期时间戳 +new Date() = new Date().getTime() 数组1. 类数组转数组 var arr = Array.prototype.slice.call(argum ...