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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- Android——基于监听器的事件处理(转)
事件,我们并不陌生! 所有的基于UI的应用程序,事件都变得不可或缺!试想一下,如果我们做的程序单击按钮和其它控件都没有反应,那么就如同一个人在这个世界上听不到声音一样! Android为我们提供了两种 ...
- 应用层timer_libc_posix timer
应用层除了通过setitimer/getitimer设置获取timer外,还可通过timer_create()等一系列函数实现应用层timer功能. 应用流程 The timers created b ...
- BusyBox getty
linux的登录主要是由两个文件在控制,/usr/sbin/getty来获得用户名,并进行检查用户名是否存在,然后将用户名传递给/usr/bin/login来获取用户输入密码和检查密码是否正确. 所以 ...
- 爱快AP-H1使用方法及排错
ikuai云平台:https://yun.ikuai8.com/login 底下原文地址:http://bbs.ikuai8.com/thread-25939-1-1.html 前文:使用爱快AP-H ...
- PHP程序员的10个有用的技巧和教程
PHP被定义为一个通用的服务器端脚本语言,它基本上是专为Web开发的重要目的.借助PHP可以创建动态和惊人的网页效果,他是被嵌入在一个HTML源文件的服务器端的脚本语言之一.因此,它成为最流行的,也是 ...
- Laravel Debugbar
Installation Require this package with composer: composer require barryvdh/laravel-debugbar After up ...
- MySql C++调用库Connector/c++编译 和 接口封装【一】mysql数据库安装
Connector/c++库的源文件编译,你需要先准备好以下工具: mysql数据库(编译时要依赖),boost库,cmake(生成sln工程文件),connector/c++的源文件,vis ...
- Spring下获取项目根路径--good
Spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器,正确地使用它们可以完成一些特定需求的功能.比如某些第三方工具支持通过 ${ ...
- 微信小程序——获取绑定事件元素的ID
小程序list数据带值跳转,一般直接通过设置item的id来标识或者通过设置键值data-xxxx的方式标识.如下图所示: 解析出来的结果如下图: 我们看到它在元素上绑定了一个checkSchoolL ...
- spring只是一个框架
想跟着 spring in action 4 系统的研究下spring,结果发现忘了怎么建一个spring项目. 关键是,不知道该建一个什么项目,Java项目?Maven项目(Java项目?Web项目 ...