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 ...
随机推荐
- SQL server 2008 建立新用户
进入Instance下的‘安全性’->登录名, 可以选择SQL server 验证,‘强制实施密码策略’.’状态‘ 为启用. 在使用的数据库下‘安全性’->登录名,选择‘登录名’,输入之前 ...
- SQL跨服务器操作语句
--简单的跨服务器查询语句 select * from opendatasource('SQLOLEDB', 'Data Source=192.168.0.1;User ID=sa;Password= ...
- thinkphp,javascript跨域请求解决方案
javascript跨域请求解决方案 前言 对于很多前端或者做混合开发的同学,我们难免会遇到跨域发起请求业务,比如A站点向B站点请求数据等等.由于最近要做一个站点集群的项目,所以具体业务要求很多个站点 ...
- 【BUG】---ionic tab-demo项目在modal页跳转URL改变页面不刷新,手动刷新后显示空白
问题描述: 项目是基于ionic tab的demo,在modal上访问其他页面,地址栏变化了,但是页面不动没刷新,自己手动刷新呢,还是空白,可是访问的页面时有内容的啊 错误: 我的路由配置 .stat ...
- Mac OSX系统搭建React natvie for android 开发环境
1.下载node.js https://nodejs.org/en/ 下载OSX版本 V5.6 2.安装android SDK JDK 3.安装react-native-cli 打开命令行,输 ...
- linux中Apache更Nginx环境配置教程
想把Apache2.2换Nginx已经有些日子,今天给动手了.找了个稳定版本:1.4.1 http://nginx.org/download/nginx-1.4.1.zip 配置很简单,就是ngin ...
- 对索引像素格式的图片进行Setpixel(具有索引像素格式的图像不支持SetPixel)解决方案
最近编写了一个验证码识别软件.其中对png.jpg图片进行二值化处理时,出现了错误:具有索引像素格式的图像不支持SetPixel解决方案.从字面上来看,这说明我对一个具有索引色的图片进行了直接RGB颜 ...
- UI3_UITableViewDelete(多选)
// AppDelegate.m // UI3_UITableViewDelete(多选) // // Created by zhangxueming on 15/7/14. // Copyright ...
- 安装Apache(httpd服务)
安装Apache(httpd服务) ① 移动所有压缩包到root文件夹下(root的家) ② 解压httpd压缩包(.tar.gz) 使用tar指令解压.tar.gz压缩包 tar 指令 -zxf : ...
- A@2a139a55 结果产生的原因
程序代码: public class ExplorationJDKSource { /** * @param args */ public static void main(String[] args ...