[leetcode]173. 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
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator { private TreeNode cur;
private Stack<TreeNode> stack; public BSTIterator(TreeNode root) {
cur = root;
stack = new Stack<>(); } /** @return whether we have a next smallest number */
public boolean hasNext() {
if(!stack.isEmpty()|| cur!=null) return true;
return false;
} /** @return the next smallest number */
public int next() {
while(cur!=null){
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
int val = cur.val;
cur = cur.right;
return val;
}
} /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/
[leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器的更多相关文章
- 173 Binary Search Tree Iterator 二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...
- Leetcode173. Binary Search Tree Iterator二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 注意: next() 和hasNext() 操作的时间复杂度是O(1),并 ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [Leetcode] Recover binary search tree 恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- Unreal Engine 4(虚幻UE4)GameplayAbilities 插件入门教程(五)技能属性集(AttributeSet)
如果没有完成前面的教程,请前往学习.先上一段理论介绍(源于https://wiki.unrealengine.com/GameplayAbilities_and_You#GameplayTasks): ...
- Github提交Spark代码
记录下提交过程,易忘供查询用.内容源自田总的分享. 1)在github上fork一份最新的master代码 2)用社区代码库创建本地仓库 git clone https://github.com/ap ...
- Xshell批量导入IP地址
我的xshell被覆盖了~~~结果原来的host没了,很郁闷要一个一个添加,网上找了很长时间在Xshell中批量添加IP的文章,结果都不行. 最后找到了https://blog.netsarang.c ...
- python之socket编写
Socket 类型 套接字格式: socket(family,type[,protocal]) 使用给定的地址族.套接字类型.协议编号(默认为0)来创建套接字. socket类型 描述 socket. ...
- uva-10004-俩色图验证
题意: 在1976年,四色猜想被一个计算机助手提出,这个理论表示对任意一个地图的上色都只需要四种颜色,地图内每一个区块和相邻的区块颜色都不相同.你现在被要求解决一个相似但相对简单的问题.给你任意一个连 ...
- spring-boot + mybatis +pagehelper 使用分页
转自:https://segmentfault.com/a/1190000015668715?utm_medium=referral&utm_source=tuicool 最近自己搭建一个sp ...
- jsp 回车代替tab 自动切换text焦点
方法一keyCode (IE11以后失效) <html> <head> <meta http-equiv="Content-Type" content ...
- getitem, setitem, delitem (把类实例化成字典的类型)
class Foo(object): def __init__(self): self.data = {} def __getitem__(self, key): ...
- as3 加载gif loader
as3原生不支持gif动态图 loader 加载gif ,内容只是以bitmap加载进来 需要动态,另外衍生类: https://files.cnblogs.com/files/dt1991/GifL ...
- 面试题-------SSL协议简介
SSL协议简介 SSL简介 Secure Socket Layer,为Netscape所研发,用以保障在Internet上数据传输之安全,利用数据加密(Encryption)技术,可确保数据在网络上之 ...