LeetCode——Binary Search Tree Iterator
Description:
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.
实现一个迭代器来遍历二叉查找树。当然首先想到的方法就是中序遍历把有序元素保存在容器中,顺序操作。但是要求uses O(h) memory就不能这样干了。
这里用一个栈来做暂存器,先把所有左节点压栈,这时栈定就一定是最小的元素。之后出栈返回当前的栈顶元素,对于栈顶元素,把以其为顶点的右子树的所有左节点都压栈。以此类推,直到所有元素都遍历一遍。在uses O(h) memory的要求下完成了二叉查找树的遍历。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator {
public Stack<TreeNode> stack; public BSTIterator(TreeNode root) {
stack = new Stack<TreeNode>();
while(root != null) {
stack.push(root);
root = root.left;
} } /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.empty();
} /** @return the next smallest number */
public int next() { TreeNode node = stack.peek();
int nextVal = node.val;
stack.pop();
TreeNode t = node.right;
while(t != null) {
stack.push(t);
t = t.left;
} return nextVal;
}
} /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/
LeetCode——Binary Search Tree Iterator的更多相关文章
- LeetCode: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- LeetCode Binary Search Tree Iterator
原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...
- [LeetCode] Binary Search Tree Iterator 深度搜索
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- leetcode Binary Search Tree Iterator python
# Definition for a binary tree node # class TreeNode(object): # def __init__(self, x): # self.val = ...
- 【leetcode】Binary Search Tree Iterator
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 【LeetCode】173. Binary Search Tree Iterator (2 solutions)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- leetcode-173:Binary Search Tree Iterator(Java)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- Nhibernate 一对一,一对多,多对多 成功映射
前语: 在Nhibernate xml 的文件配置上,一对一和多对多的配置比较简单,容易出错的反而是一对多(多对一)上. 1.一对一关联关系的映射: <one-to-one name=" ...
- centos7+ docker1.12 实践部署docker及配置direct_lvm
前言 Docker现在在后端是那么的火热..尤其当笔者了解了docker是什么.能做什么之后,真的是感觉特别的exciting,便迫不及待的去实践部署一下. 但是在实际部署中,因为笔者使用的是阿里云e ...
- 专题实验 Statspack & 9大动态视图
statspack 是一个DBA经常用的调优工具, 它的主要作用是, 针对数据库的不同时刻做快照, 然后来比对快照之前的差异和瓶颈, 快照可以是手动的也可以是自动的, 从 oracle 10g开始, ...
- CI循环数组问题
当我们在Controll中把数据传递到view中如: $data['cates_data']=$this->Category_Model->byid_data($id); #调用模型层查询 ...
- LoadRunner性能分析指标解释
Transactions(用户事务分析) 用户事务分析是站在用户角度进行的基础性能分析. 1.Transation Sunmmary(事务综述) 对事务进行综合分析是性能分析的第一步,通过分析测试时间 ...
- 如何查询表A中的某字段的值在表B中不存在?
1.测试表创建,插入数据: create table a (id int, name )); create table b (id int); ,'a'); ,'b'); ,'c'); ,'d'); ...
- 关于Java方法的参数
刚好看到C++的函数这块,说C++中除了引用类型的形参,其他都是实参的副本(个人总结). 隐约记得Java中方法的参数也是这么回事,于是手动测试一番. 结果 Java中方法的参数都是值传递,哪怕是引用 ...
- Android4.4 Framework分析——getContentResolver启动ContentProvider的过程
ContentProvider的创建通常是在第一次使用的时候. 没时间分析,可參考老罗的分析 http://blog.csdn.net/luoshengyang/article/details/696 ...
- erlang 自定义函数的初步应用
一.模块内调用 1> AA=fun(BB)-> io:format("this is test args ~s~n",[BB]) end.#Fun<erl_eva ...
- Android SDK的安装教程
Android4.1虽说已经发布了好些天,但由于的我手机比较坑,系统依旧保持在2.3.4.0的都是可望不可即的了,就别说4.1.由于资金的问题,没法换手机,只能另想方法,通过在笔记本上装andro ...