leetcode-173:Binary Search Tree Iterator(Java)
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.
要求:写一个二叉查找树,每次返回树中的下一个最小节点
比如上图中的二叉查找树,从根节点开始,依次返回1,3,4,6,7... ...
思路:维护一个栈,先将根结点的左子树全部压栈,每次弹出栈顶元素,若某次弹出的栈顶元素有右子树,比如3,此时需要将以该节点的右子树为根的子树的左子节点全部压栈
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ import java.util.Stack;
public class BSTIterator {
Stack<TreeNode> stack = new Stack<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.isEmpty(); } /** @return the next smallest number */
public int next() {
TreeNode minCurrent = stack.pop();
if(minCurrent.right != null){
TreeNode rightNode = minCurrent.right;
while(rightNode != null){
stack.push(rightNode);
rightNode = rightNode.left;
}
} return minCurrent.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(Java)的更多相关文章
- LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 【leetcode】Binary Search Tree Iterator(middle)
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 ...
- 【LeetCode】704. Binary Search 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性查找 二分查找 日期 题目地址:https:// ...
- 【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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 【leetcode】Binary Search Tree Iterator
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] 272. Closest Binary Search Tree Value II 最近的二叉搜索树的值 II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
随机推荐
- 将HTML表格导出到EXCEL,兼容Firefox,支持中文
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [引]MSDN Visual Basic 和 C# 中都会用到的编程概念
本文转自:http://msdn.microsoft.com/zh-cn/library/dd460655.aspx 本节介绍 Visual Basic 和 C# 中都会用到的编程概念. 本节内容 ...
- 关于Git补丁文件交互
之前各个章节的版本库的交互都是通过 git push和git pull命令来实现的.这个是Git最主要的交互模式,但并不是全部. 使用补丁文件是另外一种交互方式,适用于参与者众多的大型项目进行的分布式 ...
- Greedy
在现实世界中,有这样一类问题:它有n个输入,而它的解就由这n个输入的某个子集组成,不过这个子集必须满足某些事先给定的条件.把那些必须满足的条件称为约束条件:而把满足约束条件的子集称为该问题的可行解.问 ...
- 基于mvc结构的前端页面框架搭建
前端开发一年了,向大家交流下自己实践总结下来的一点点开发心得.人生难免磕磕碰碰,前进的道路很多,在学习工作上我们都得学会如何让自己过的更高效,代码亦是如此. 下面,开始介绍自己总结的前端框架搭建(布局 ...
- 如何让Div层悬浮在Flash Object对象之上(转载)
今天有个用户,门户右上角的倒三角登陆小按钮在他的电脑上无法显示,他用的笔记本屏幕较小,宽度正好显示出页面内容,经查看,门户页眉使用的为flash对象. 大家都知道,如果想让某个图片或者Div层悬浮在别 ...
- java 网络API访问 web 站点
package cn.magicdu.think.socket; import java.io.BufferedReader; import java.io.InputStreamReader; im ...
- linux时间自动同步
1,修正本地时区及ntp服务 #yum -y install ntp#rm -rf /etc/localtime#ln -s /usr/share/zoneinfo/Asia/Shanghai /et ...
- eclipse中启动Genymotion模拟器的错误
错误程序: Output file: C:\Users\wishwzp\.genymotion-eclipse.logLoading Genymotion libraryGenymotion dire ...
- GDI+中发生一般性错误之文件被占用
有多种原因可能导致这个异常出现,比如创建文件的权限不足.文件被占用等. 这里提供一个使用Stream读取图片避免文件被占用的方法. public Image GetImageFromStream(st ...