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 ...
随机推荐
- Charles 应用
1.首先设置本机host文件,将需要测试的域名指向127.0.0.1:(测试域名没有代理,才会这么操作): 2.打开Charles,设置Proxy选项,取消“Windows Proxy”选中状态: 3 ...
- C#用反射判断一个类型是否是Nullable同时获取它的根类型(转自网络)
在我们的应用程序中我们使用类描述我们的业务对象,为我们产生一些报表之类的,那就依赖大量不同的对象,我们创建一个帮助方法来转换我们的业务对象,或是一个List的业务对象到DataTables. 由于数据 ...
- Js中把JSON字符串转换为JSON对象(eval()、new Function())
在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 第一种解析方式:使用eval函数来解析,并且使用j ...
- Windows下搭建论坛
Windows下搭建论坛 真正的O基础架构,一步一步走向成功 转载请注明原作者出处 环境准备篇 安装集成包软件 解压后如下 以管理员身份运行setup的批处理 选择推荐的apache版本 选择推荐的m ...
- iOS 自定义view里实现控制器的跳转
1.view里实现控制器的modal 拿到主窗口的根控制器,用根控制器进行modal需要的modal的控制器 场景:点击自定义view里的按钮实现控制器的modal UIViewController ...
- FastSocket学习笔记~制定自已的传输协议~续~制定基于FastSocket的协议
FastSocket这个东西上次我已经说过,它使用简单,功能强大,扩展灵活,目前在新浪的生产环境中已经被广泛使用,所以它的性能,安全等各方面我们绝对可以信赖,今天我们来说一个话题,和上一讲有关,这次我 ...
- 20141201--测试 jQuery
测试 JavaScript 框架库 - jQuery jQuery 是一个 JavaScript 库. 引用 jQuery <!DOCTYPE html> <html> < ...
- 20141104--SQL连接查询,联合查询
---------------------------连接查询-------------------------------- --横向连接查询 --可以将子查询放在from之前,用来替换显示出来的信 ...
- 20141030--SQL2008常用命令-1
create database biao2--创建新的数据库 go use biao2 go create table shuiguo--创建表shuiguo ,create table创建表 ( 序 ...
- Placeholdem文本域占位符符号标识JavaScript插件
Placeholdem是文本域占位符符号标识的一个JavaScript插件.占位符的值将逐步删除焦点文字,并在焦点离开逐步恢复. 在线demo:http://placeholdem.jackrugil ...