LintCode-Implement Iterator of Binary Search Tree
Design an iterator over a binary search tree with the following properties:
- Elements are visited in ascending order (i.e. an inorder traversal)
- next() and hasNext() queries run in O(1) time in average.
For the following binary search tree, inorder traversal by using iterator is [1, 6, 10, 11, 12]
10
/ \
1 11
\ \
6 12
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
* Example of iterate a tree:
* Solution iterator = new Solution(root);
* while (iterator.hasNext()) {
* TreeNode node = iterator.next();
* do something for node
* }
*/
public class Solution {
private Stack<TreeNode> nodeStack; //@param root: The root of binary tree.
public Solution(TreeNode root) {
nodeStack = new Stack<TreeNode>();
//Initialize first, then determine null, otherwise, the hasNext() function will cause problem.
if (root==null) return;
nodeStack.push(root);
TreeNode cur = root.left;
while (cur!=null){
nodeStack.push(cur);
cur = cur.left;
}
} //@return: True if there has next node, or false
public boolean hasNext() {
if (nodeStack.isEmpty()) return false;
else return true;
} //@return: return next node
public TreeNode next() {
if (nodeStack.isEmpty()) return null;
TreeNode next = nodeStack.pop();
if (next.right==null) return next;
else {
nodeStack.push(next.right);
TreeNode cur = next.right.left;
while (cur!=null){
nodeStack.push(cur);
cur = cur.left;
}
return next;
}
}
}
LintCode-Implement Iterator of Binary Search Tree的更多相关文章
- Lintcode: Remove Node in Binary Search Tree
iven a root of Binary Search Tree with unique value for each node. Remove the node with given value. ...
- 【Lintcode】095.Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- [Lintcode]Inorder Successor in Binary Search Tree(DFS)
题意 略 分析 1.首先要了解到BST的中序遍历是递增序列 2.我们用一个临时节点tmp储存p的中序遍历的下一个节点,如果p->right不存在,那么tmp就是从root到p的路径中大于p-&g ...
- [LeetCode] 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 设计迭代器(搜索树)--------- java
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】Binary Search Tree Iterator(middle)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 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
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
随机推荐
- NPOI--操作Excel之利器(一)
最近在做一个产品配置的项目,类似于京东上的自主装机,也就是根据自己的需要配置一套完整的产品,只不过我们做的是一个网络产品的配置,如路由器,交换机等网络设备.配置完成后会将配置的信息导出到Excel中, ...
- Android OpenGL ES(三)----编程框架
首先当然是创建Android项目,你可以选择最新的Android Studio也可以选择eclipse都是一样的.我们重点讲解开发OpenGL ES的流程 1.定义顶点着色器和片段着色器 第一节我们讲 ...
- Android DiffUtil
Android 的recyclerview-v7:24.2.0 发布后多了个DiffUtil工具类,这个工具类能够大大解放了Android开发者的一个苦恼:RecyclerView局部刷新和重新刷新时 ...
- Python Opearte MS-SQL Use Pymssql
看到很多的开源数据库会用到MySQL,Python同样也使用,但是我已经习惯使用图形化界面,操作感极强的MS-SQL 看到Python也提供MS-SQL连接方式,需要用到PyMssql. 在Windo ...
- 通过带参数的Sql语句来实现模糊查询(多条件查询)
#region 通过带参数的Sql语句来实现模糊查询(多条件查询) StringBuilder sb = new StringBuilder("select * from books&quo ...
- table表格实现点击修改 PHP同步数据库 排序
最近几天在做一个网站,牵扯到一个导航管理的功能!领导说不用作,可是由于自己自作主张,搞了1天的功能.领导说这个导航管理就是不用做!容易牵扯出好多问题来!估摸是客户小的原因! 没办法就把我1天的劳动荒废 ...
- MySQL写入插入数据优化配置
*innodb_buffer_pool_size 如果用Innodb,那么这是一个重要变量.相对于MyISAM来说,Innodb对于buffer size更敏感.MySIAM可能对于大数据量使用默认的 ...
- C#高效率导出Excel
首先,需要引用excel的库: Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Exce ...
- BeanDefinition的Resource定位
1.以编程的方式使用DefaultListableBeanFactory时,首先定义一个Resource来定位容器使用的BeanDefiniton.这时使用的是ClassPathResource,这意 ...
- C++学习——类的继承
公有继承(public).私有继承(private).保护继承(protected)是常用的三种继承方式. 1. 公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时, ...