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 ...
 
随机推荐
- Python基础:1.数据类型(空、布尔类型、整型、长整型、浮点型、字符串)
			
提示:python版本2.7,windows系统 Python提供的基本数据类型:空.布尔类型.整型.长整型.浮点型.字符串.列表.元组.字典.日期 1.空(None) None,是一个特殊的值,不能 ...
 - vagrant Ubuntu server 12.04 dpkg: dependency problems prevent configuration of python-gi
			
Ubuntu server 12.04因为尝试安装过xfce,导致sudo apt-get install xxx 都会返回,如: vagrant@precise32:~$ sudo apt-get ...
 - php gd 生成日历图
			
<?php //如果您提交了时间则显示您提交年月的日历,否则显示当前月份日历 if (isset($_GET['month']) && isset($_GET['year'])) ...
 - postgresql 将同一个字段的值组合和将多个字段的值组合
			
多字段值根据连接符拼接 concat_ws(':',aaa,bbb) 单字段值根据连接符拼接 string_agg(ccc,' \r\n ') 如果要将多个字段的值拼接成一个: string_agg( ...
 - LINQ to Entities 不支持 LINQ 表达式节点类型“Invoke”
			
解决方法即 where后加 .Compile()
 - php的标记形式
			
共三种: 推荐第一种,第三种需要在php.ini中配置 效果: 第三种配置 将short_open_tag=Off改为On重启Apache就可以了
 - linux之let用法
			
shell程序中的操作默认都是字符串操作,在要运行数学运算符的时候可能得到意想不到的答案: var=1 var=$var+1 echo $var output:1+1 从这个例子中可以看出shell字 ...
 - selenium文件上传的实现
			
一.对于上传文件, 从手动操作我们可以看出, 需要对window 窗体进行操作, 而对于selenium webdriver 在这方面应用就受到了限制. 但是, 庆幸的是, 对于含有input ele ...
 - 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
			
中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030 内容详见: http://www.360doc.com/content/11/1004/12/6139921_1 ...
 - C++ IO 详细用法
			
http://www.cnblogs.com/keam37/ keam所有 转载请注明出处 本文将分别从<iostream>,<sstream>,<fstream> ...