Java for 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.
解题思路:
其实就是按照中序遍历的顺序出列,有两种实现思路:
一、构造BST的时候直接预处理下,构造出一条按照中序遍历的list,每次读取list的元素即可。
二、不进行预处理,按照中序遍历的思路使用Stack动态的进行处理
这里我们实现思路二:
public class BSTIterator {
private Stack<TreeNode> stack=new Stack<TreeNode>();
public BSTIterator(TreeNode root) {
if (root != null)
pushLeft(root);
}
/** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.isEmpty();
}
/** @return the next smallest number */
public int next() {
TreeNode top = stack.peek();
stack.pop();
if(top.right!=null)
pushLeft(top.right);
return top.val;
}
public void pushLeft(TreeNode root) {
stack.push(root);
TreeNode rootTemp = root.left;
while (rootTemp != null) {
stack.push(rootTemp);
rootTemp = rootTemp.left;
}
}
}
Java for LeetCode 173 Binary Search Tree Iterator的更多相关文章
- ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...
- leetcode 173. Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 【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】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 ...
- 173. Binary Search Tree Iterator
题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with th ...
随机推荐
- bootstrap_UI
- CSS文字排版
一.font-size 我来试一试:为第一段中的“胆小如鼠”设置字号为:20px,字体颜色为:red. <!DOCTYPE HTML> <html> <head> ...
- 【bzoj2463】 谁能赢呢?
www.lydsy.com/JudgeOnline/problem.php?id=2463 (题目链接) 题意 一个n*n的棋盘,开始时左上角有一个棋子,每次可以把棋子向4个方向移动,但不能移动到曾经 ...
- 表单form action的url写法
在写web页面时,标签 是很常见的元素,它的一个属性是action,用来标识将表单交给谁去处理.很显然,这里有一个地址的问题,而且是在服务器这边的地址.比如服务器内的一个servlet. 那么这个 ...
- web端测试和移动端测试的区别小记
转:http://qa.blog.163.com/blog/static/19014700220157128345318/ 之前一直参与web端的测试,最近一个项目加入了移动端,本人有幸参与了移动端的 ...
- eclipse中文乱码问题解决方案
eclipse之所以会出现乱码问题是因为eclipse编辑器选择的编码规则是可变的.一般默认都是UTF-8或者GBK,当从外部导入的一个工程时,如果该工程的编码方式与eclipse中设置的编码方式不同 ...
- shared_ptr<> reset
// std_tr1__memory__shared_ptr_reset.cpp // compile with: /EHsc #include <memory> #include < ...
- 织梦DedeCms网站更换域名后文章图片路径批量修改
因为织梦上传图片用的是绝对地址,如果域名更换后,之前发布的文章的图片URL是不会跟着改变的,所以我们需要把旧域名替换成新的域名,方法很简单,有一段SQL语句更新一下文章正文内容就行. 复制下面SQL语 ...
- 锋利的jQuery-4--animate()的用法
1.一般动画: $("btn").click(function(){ $("div").animate({"left" : "+= ...
- linux cp命令参数及用法详解
cp (复制档案或目录)[root@linux ~]# cp [-adfilprsu] 来源档(source) 目的檔(destination)[root@linux ~]# cp [options] ...