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 ...
随机推荐
- 白话空间统计之四:P值和Z值(上):零如果
本来今天想要讲讲软件操作的,后来发现好像还有好几个重要的指标没有说,干脆等所有说完在讲操作吧.否则操作出来的结果会发现大量的"不明觉厉". 首先是空间统计里面非常神奇的两个值:P值 ...
- ES文件浏览器 WIFI 查看电脑文件怎么弄
1 开启来宾账户 2 右击要共享的文件夹,添加Guest共享(如果只是要查看共享的资源,权限级别为读取即可) 3 共享之后,网络路径就是"\\"+你的计算机名+" ...
- httpclient发送get请求
/** * 获取httpclient的请求url地址 */ public static String getUrl(){ String url = "http://"+map.ge ...
- 【转载】HTTP协议与WEB本质
当你在浏览器地址栏敲入"http://www.csdn.net/",然后猛按回车,呈现在你面前的,将是csdn的首页了(这真是废话,你会认为这是理所当然的).作为一个开发者,尤其是 ...
- Oracle11g表空间导入dmp数据
如果你的表数据没有附带表空间和用户名,那么只要一句话 Imp {u_name}/{u_pwd}@{local_svrname} fromuser={from_user} touser={u_name} ...
- 嵌入式开发之davinci--- 8168 电源调试总结
http://www.61ic.com/Article/DaVinci/TMS320DM81x/201403/51863.html
- 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM(转载)
http://www.cnblogs.com/indream/p/3602348.html 刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code ...
- PyTorch 高级实战教程:基于 BI-LSTM CRF 实现命名实体识别和中文分词
前言:译者实测 PyTorch 代码非常简洁易懂,只需要将中文分词的数据集预处理成作者提到的格式,即可很快的就迁移了这个代码到中文分词中,相关的代码后续将会分享. 具体的数据格式,这种方式并不适合处理 ...
- CAS和ABA
1 CAS compare and swap的缩写,详见乐观锁和悲观锁. 2 ABA 就是说,我获取的旧值是A,然后被人修改成了B,但是又被人修改成了A,我就认为并没有修改,更新内存. 解决办法,给每 ...
- id 查询
Ids Query | Elasticsearch Reference [6.2] | Elastic http://www.elastic.co/guide/en/elasticsearch/ref ...