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.

Show Tags
Have you met this question in a real interview?

Yes
No
 

Discuss

SOLUTION 1:

使用inorder traversal把tree转化为arraylist.

递归

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator {
ArrayList<TreeNode> list;
int index; public BSTIterator(TreeNode root) {
list = new ArrayList<TreeNode>();
iterator(root, list); index = 0;
} // solution 1: recursion.
public void dfs (TreeNode root, ArrayList<TreeNode> ret) {
if (root == null) {
return;
} //Use inorder traversal.
dfs(root.left, ret);
ret.add(root);
dfs(root.right, ret);
} /** @return whether we have a next smallest number */
public boolean hasNext() {
if (index < list.size()) {
return true;
} return false;
} /** @return the next smallest number */
public int next() {
return list.get(index++).val;
}
} /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/

SOLUTION 2:

the iterator version.

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator {
ArrayList<TreeNode> list;
int index; public BSTIterator(TreeNode root) {
list = new ArrayList<TreeNode>();
iterator(root, list); index = 0;
} // solution 2: Iterator.
public void iterator (TreeNode root, ArrayList<TreeNode> ret) {
if (root == null) {
return;
} Stack<TreeNode> s = new Stack<TreeNode>();
// bug 1: use push instead of put
TreeNode cur = root; while (true) {
// bug 2: should push the node into the stack.
while (cur != null) {
s.push(cur);
cur = cur.left;
} if (s.isEmpty()) {
break;
} // bug 3: should pop a node from the stack.
// deal with the top node in the satck.
cur = s.pop(); // bug 2: should be cur not root.
ret.add(cur);
cur = cur.right;
}
} /** @return whether we have a next smallest number */
public boolean hasNext() {
if (index < list.size()) {
return true;
} return false;
} /** @return the next smallest number */
public int next() {
return list.get(index++).val;
}
} /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/

LeetCode: Binary Search Tree Iterator 解题报告的更多相关文章

  1. 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...

  2. 【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告

    时间挤挤总是有的 太久不做题,脑子都生锈了.来道水题练练手 题目地址: https://leetcode.com/problems/binary-search-tree-iterator/ 题目内容: ...

  3. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  4. LeetCode Binary Search Tree Iterator

    原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...

  5. LeetCode——Binary Search Tree Iterator

    Description: Implement an iterator over a binary search tree (BST). Your iterator will be initialize ...

  6. [LeetCode] Binary Search Tree Iterator 深度搜索

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  7. 【LeetCode】270. Closest Binary Search Tree Value 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  8. leetcode Binary Search Tree Iterator python

    # Definition for a binary tree node # class TreeNode(object): # def __init__(self, x): # self.val = ...

  9. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

随机推荐

  1. Using PHP as a Spring MVC View via Quercus(转)

    原贴: http://blog.caucho.com/2009/04/14/using-php-as-a-spring-mvc-view-via-quercus/ This week, I’ve be ...

  2. mysql 排序 oder by 和 使用hibernate 排序

    String sql="select * from sys_invitation where to_phone = '13000000000' order by create_time de ...

  3. iptables中ULOG和NFLOG实现分析【转】

    原文地址:http://blog.csdn.net/eydwyz/article/details/52456335 ----------原文如下---------------------------- ...

  4. 看过这两张图,就明白 Buffer 和 Cache 之间区别

    Buffer常见的是这个: 对,就是铁道端头那个巨大的弹簧一类的东西.作用是万一车没停住,撞弹簧上减速慢,危险小一些.叫缓冲. Cache常见的是这个: 没错,就是一种保管箱.看到右边那个被锈掉的Fo ...

  5. 【struts2】struts2的execAndWait拦截器使用

    使用execAndWait拦截器可以在等待较长时间的后台处理中增加等待页面.实现如下图所示的效果: 1)struts.xml主要部分 <action name="test" ...

  6. MongoDB----逻辑与物理存储结构

    基本的操作 一.常用的命令和基础知识 1.进入MongoDB shell 首先我们进入到MongoDB所在目录执行 cd /work/app/mongodb/bin/ #启动 ./mongo 为了方便 ...

  7. 简单易用的安装文件制作工具NSIS的使用demo示例

    安装文件制作工具NSIS 使用总结   在给客户开发客户端软件时,为避免技术人员亲自上门安装带来额外的成本损耗,通常我们都会自作一个安装包,以确保我们开发的程序的相关依赖资源.环境在客户端运行前能自动 ...

  8. linux中WDCP的日志彻底删除技巧

    apache或nginx都有开关默认日志,一个是正常访问日志,一个是错误的日志,目录在 /www/wdlinux/nginx-1.0.15/logs /www/wdlinux/httpd-2.2.22 ...

  9. Java 8 – StringJoiner example

    In this article, we will show you a few StringJoiner examples to join String. 1. StringJoiner1.1 Joi ...

  10. Window 7 + Ubuntu 双系统安装

    硬件: ThinkPad X260 i5-6200U/8G/480G 当前系统: Window 7 旗舰版 64位 下载 Ubuntu 官网 下载桌面版,当前 Ubuntu 版本为:16.04 镜像安 ...