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 ...
随机推荐
- Table of Contents - Git
Downloading and Installing Git Download for Linux and Unix Integration with Eclipse Eclipse 提交工程至 G ...
- 二十、Android -- SDcard文件读取和保存
背景 一些东西可以 ...
- 收集一下Windows7系统啊
Windows7系统:http://down.662p.com/list/141_1.html 萝卜家园WIN7系统X86位旗舰特别GHOST版2014年12月 这个是萝卜家园WIN7系统X86位 ...
- maxlength属性在textarea里奇怪的表现
HTML5给表单带来了很多改变,比如今天要说的maxlength,这个属性可以限制输入框输入的最大字字符数,更方便的是对于粘贴的内容也能够根据字符数自动截断. 最近就接到这要一个需求,限制用户最多输入 ...
- 怎样把function中的arguments变成普通数组
当我们在写一个具有处理可变长度参数的函数时,需要对arguments做一些操作.但是arguments它并不是一个数组,没有数组的各种操作,而且,JS的严格模式中不允许更改它的值. 这时我们需要将它的 ...
- WindowsPhone8 数据库增删改查
今天第一次在博客园发表文章,如果有的地方写的不对,还请大家指出! 1.这就是一个简单wp8数据库增删改查 1.创建数据表Person [Table] public class Person : INo ...
- 《shell条件测试语句,字符串测试apache是否开启》
还得我想了10分钟才明白”!=“和"-n"的用法区别,做个笔记捋一捋 第一种方法:测试apache是否开启?字符串测试 #!/bin/bash web=`/usr/bin/pgre ...
- tp中让头疼似懂非懂的create
项目中多次用到create() 只能它是表单验证,不过好出错,痛下心扉好好了解理解它的来龙去脉和所用的用法 一:通过create() 方法或者 赋值的方法生成数据对象,然后写入数据库 $model = ...
- 图片剪裁上传插件 - cropper
图片剪裁上传插件 - cropper <style> .photo-container{float: left;width: 300px;height: 300px;} .photo-co ...
- ASP.NET MVC 应用程序的安全性,看一眼你就会了
1.使用Authorize特性登陆对于我们开发程序而言,基本上都是要求角色成员使用Authorize特性,比如,对于管理员而言角色是Admin,对于登陆注册登陆用户而言是User那么我们在用户登陆的时 ...