173 Binary Search Tree Iterator 二叉搜索树迭代器
实现一个二叉搜索树迭代器。你将使用二叉搜索树的根节点初始化迭代器。
调用 next() 将返回二叉搜索树中的下一个最小的数。
注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 O(h) 内存,其中 h 是树的高度。
详见:https://leetcode.com/problems/binary-search-tree-iterator/description/
Java实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class BSTIterator { private Stack<TreeNode> stk=new Stack<TreeNode>();
public BSTIterator(TreeNode root) {
while(root!=null){
stk.push(root);
root=root.left;
}
} /** @return the next smallest number */
public int next() {
TreeNode node=stk.pop();
int val=node.val;
if(node.right!=null){
node=node.right;
while(node!=null){
stk.push(node);
node=node.left;
}
}
return val;
} /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stk.isEmpty();
}
} /**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator obj = new BSTIterator(root);
* int param_1 = obj.next();
* boolean param_2 = obj.hasNext();
*/
参考:https://www.cnblogs.com/grandyang/p/4231455.html
173 Binary Search Tree Iterator 二叉搜索树迭代器的更多相关文章
- [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 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 ...
- [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] 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 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- LeetCode 669. Trim a Binary Search Tree修剪二叉搜索树 (C++)
题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...
随机推荐
- Deepin-文件目录介绍
请参见这篇文件:来自一个强大的网站 我主要介绍的就是: 下面所列文件,全部添加进了path目录(Linux查找命令,请参见man.linux,无论是find 或者是 which等) Deepin默认可 ...
- FZU 2150 Fire Game (暴力BFS)
[题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...
- poj1161Post Office【经典dp】
题目:poj1161Post Officeid=1160" target="_blank">点击打开链接 题意:给出一条直线上的n个坐标表示村庄的位置,然后要在上面 ...
- phpexcel不能输出中文
问题描写叙述:在使用phpexcel时,假设在单元格中填充中文内容,会导致输出单元格为空的情况,甚至连中文的字符(?!等)都无法识别. 产生原因:从网上查是utf-8的问题 解决方法:能够用iconv ...
- shutdown abort模式丢失redo,使用隐含參数启库
shutdown abort模式 丢失redo log 无法open数据库 通过告警报错ORA-00354: corrupt redo log block header 从该错误能够看出当前日志的re ...
- OFbiz实体引擎
安全可靠的数据存储是数据管理战略的关键业务,OFbiz认真对待数据管理.不把全部繁琐和easy出错的数据管理任务留给应用开发人员.OFbiz在设计和实现阶段非常好的贯彻了这个理念. 实体引擎是数据库无 ...
- Redis和Memcache性能测试对比
Redis和Memcache在写入性能上面差别不大,读取性能上面尤其是批量读取性能上面Memcache全面胜出,当然Redis也有自己的优点:比如数据持久化.支持更多的数据结构(Set List ZS ...
- 2016/3/26 weixin 头像 昵称 网页优化显示 缺表中数据 只有代码 无显示效果
weixin.php <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- ranlib
1 ranlib的缩写 random access library 2 ranlib的作用 为静态库的符号建立索引,可以加速链接,因此称用ranlib处理过的library为random access ...
- python iterable 和list、dictionary的区别和联系
1 为什么一些函数的参数指定要iterable object的,但是也可以传入list为参数? 因为list.dictionary都是iterable object. 在iterable object ...