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 ...
随机推荐
- typedef的使用3——使用经过typedef定义的函数构成的函数数组
#include <stdio.h> #include <string.h>//不加还能跑,加上反而跑不了了...笑哭 #pragma warning(disable:4996 ...
- [转]Oracle学习笔记——权限管理
本文转自:http://www.cnblogs.com/whgw/archive/2011/10/30.html 一.系统的默认用户 1)sys用户是超级用户,具有最高权限,具有sysdba角色,有c ...
- dedecms内容管理
dedecms中的内容模型是指文章.软件.商品等类型的文章字段模板.在dedecms中,文章数据由文章主表和文章附加表构成,主表存放文章公共拥有的信息,比如标题,添加时间,点击量等,文章附加表存放文章 ...
- SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库
SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库 /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...
- (八)Hibernate 映射关系
所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 第一节:Hibernate 一对一映射关系实现 1,按照主键映射: 2, ...
- Oracle之初始创建scott/tiger来测试
在redhat5.5(32bit)上安装好oracle11g数据库软件,然后安装一个数据库,再然后登录数据库,创建scott/tiger测试 首先登录数据库,这里登录时是没有启动数据库的 [oracl ...
- CodeForces 538B
Description A number is called quasibinary if its decimal representation contains only digits 0 or 1 ...
- (转)web.config 为某个单独页面设置编码格式
原文链接:http://www.cnblogs.com/mytechblog/articles/1937407.html 全站应用utf-8格式,在web.config里的<system.web ...
- 对WebClient扩展自动解压缩页面
WebClient下载压缩网页时出现的全是乱码,可通过扩展来解决这个问题. public class MyWebClient : WebClient { protected override WebR ...
- SQL索引问题
很多文章都提到使用IN,OR会破坏索引,造成全表扫描,但实际测试却不是这样. ) 或者 ,) 以上SQL文,第一组(=,IN),第二组(=,OR,IN),每一组的两个SQL文都使用相同的执行计划,执行 ...