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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- java资料——哈希表(散列表)(转)
哈希表 散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度. ...
- Hive shell 命令
Hive shell 命令. 连接 hive shell 直接输入 hive 1.显示表 hive> show tables; OK test Time taken: 0.17 seconds, ...
- [转]eclipse导入V7包出现错误解决办法
android下v4 v7 v21等包是android系统的扩展支持包,就想windows的系统补丁一个道理. android的扩展包主要是用来兼容低版本的,比如android3.0以后出现 ...
- Axel 快速下载
Axel 是一个轻量级下载程序,它和其他加速器一样,对同一个文件建立多个连接,每个连接下载单独的文件片段以更快地完成下载. Axel 支持 HTTP.HTTPS.FTP 和 FTPS 协议.它也可以使 ...
- 实现整数转化为字符串函数itoa()函数
函数原型: char *itoa( int value, char *string,int radix);原型说明:value:欲转换的数据.string:目标字符串的地址.radix:转换后的进制数 ...
- easyui中datagrid用法,加载table数据与标题
加载标题写法: 多行标题:columns: [[ columns: [[ { field: 'itemid', title: 'Item ID', rows ...
- loadrunner循环执行某个动作
1.action部分定义 int i; int count; 2. 打算循环的代码前代码如下: count=rand() % 8 +1; for(i=0;i<coun ...
- thinkphp openfire 添加用户 骨架
public function addadd(){ header("Content-Type:text/html; charset=utf-8"); $user=$_POST['n ...
- android Fragment 笔记
Fragment多用于平板中,Fragment当成Activity的一个界面的一个组成部分,Fragment有自己的生命周期,但是必须依托在Activity中. 参考链接 https://develo ...
- MYSQL 两日期之间的工作日(除去周六日,不考虑节假日)
select (floor(days/7)*5+days%7 -case when 6 between wd and wd+days%7-1 then 1 else 0 end - ...