【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告
时间挤挤总是有的
太久不做题,脑子都生锈了。来道水题练练手
题目地址:
https://leetcode.com/problems/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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
题目解析:
BST中序遍历,难点(算么 =。=)在于不可以简单一次递归完成,而是需要停止继续机制。
注意到要求时间均摊复杂度O(1),和不超过树高的空间复杂度。
不妨定义一个栈如下:
LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
在此维护一个性质:栈顶的元素是当前的最小元素。
因此,初始化栈的时候,需要把root的所有左节点压入栈中。每当请求next的时候,就返回栈顶节点的值,同时,如果栈顶节点有右节点,就需要把右节点和他所有的左子节点都压入栈中。
具体代码:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator { LinkedList<TreeNode> stack = new LinkedList<TreeNode>(); // 在处理栈顶这个节点以前,他的左子树已经被处理过了,换言之,栈顶必然是当前最小节点 public BSTIterator(TreeNode root) { while (root != null) {
stack.push(root);
root = root.left;
} } /**
* @return whether we have a next smallest number
*/
public boolean hasNext() {
return stack.size() != 0;
} /**
* @return the next smallest number
*/
public int next() {
TreeNode node = stack.pop();
if (node.right != null) {
TreeNode left = node.right;
while (left != null) {
stack.push(left);
left = left.left;
}
}
return node.val;
}
} /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/
复杂度分析:
可以看出,任意时刻,栈中的元素数量不会超过树高。这个画一个图就明白了。
由于每个节点会被压入栈中 && 被访问值一次,所以对每个节点的操作数都是2,虽然访问某些特定节点时,需要更多的压栈操作,但由于next方法可能只执行一次访问值操作,所以实际上更多的压栈操作是分摊到了不需要压栈的时刻中了。
所以均摊是O(1)。
【原创】leetCodeOj --- 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】173. Binary Search Tree Iterator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...
- 【LeetCode】270. Closest Binary Search Tree Value 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【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(Java)
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 ...
- 二叉树前序、中序、后序非递归遍历 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 for LeetCode 173 Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- Swift - UIColor使用自定义的RGB配色
1,比如rgb 色值为55. 186 .89 那么给UIColor设置里面要除以255 1 UIColor(red: 55/255, green: 186/255, blue: 89/255, alp ...
- 怎样将baidu地图中的baidu logo 去掉
今天我的老大问我是不是能够将baidumap 的js版中baidu logo 去掉. 我上网查询一下,有各种方法.比方将相应的logo div remove hide 等等,这些都是须要JS 函数触发 ...
- ajax文本空输入显示用户信息
一般文件代码 public void ProcessRequest (HttpContext context) { //获取主见值 string s = context.Request["u ...
- Linux目录结构和常用命令
源地址:http://www.cnblogs.com/JCSU/articles/2770249.html 一.Linux目录结构 你想知道为什么某些程序位于/bin下,或者/sbin,或者/usr/ ...
- IT痴汉的工作现状16-职业发展
回首多年来的工作经历.发现自己的职业发展真是太平庸只是了.就像我的名字张伟,平淡无奇.而我,还是几年前刚入职模样的我,仍然像个涉世未深的矛头小子,相信技术能够改变世界.真是一入IT深似海,为伊消得人憔 ...
- POJ2112_Optimal Milking(网洛流最大流Dinic+最短路Flody+二分)
解题报告 农场有k个挤奶机和c头牛,每头牛到每一台挤奶机距离不一样,每台挤奶机每天最多挤m头牛的奶. 寻找一个方案,安排每头牛到某一挤奶机挤奶,使得c头牛须要走的全部路程中的最大路程的最小值. 要使每 ...
- Spring MVC的异步模式
高性能的关键:Spring MVC的异步模式 我承认有些标题党了,不过话说这样其实也没错,关于“异步”处理的文章已经不少,代码例子也能找到很多,但我还是打算发表这篇我写了好长一段时间,却一直没发表 ...
- 世界gis相关的资源网站分类整理
********************首先介绍个新颖的GIS论坛——GIS520论坛******************** GIS520论坛(共享地信学习资源的专业论坛) www.gis520.c ...
- 分享毕业学生“ERP实施project联赛”总结,是肺腑之言——知识是人的价值的体现,每门课程是有意义的学校纪律
丁.这是我刚刚完成的实习报告,特别是给你一个.阿信,让你知道的真实想法研究生管,我希望你相信在教育管帮助.---雷管1102 刘弈福 以上是刚刚收到(20140427)生邮件,贻富不是我带的毕业设计学 ...
- CentOS Kernel Source Install
http://linuxmoz.com/centos-kernel-source-install/