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. ss is one another utility to investigate sockets(特适合大规模tcp链接)

    原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: ss is one another utility to investigate sockets(特适合大规模tcp链接) 具体的可以 ...

  2. 原创工具binlog2sql:从MySQL binlog得到你要的SQL

    从MySQL binlog得到你要的SQL.根据不同设置,你可以得到原始SQL.回滚SQL.去除主键的INSERT SQL等. 用途 数据回滚 主从切换后数据不一致的修复 从binlog生成标准SQL ...

  3. mariadb/mysql配置允许远程访问方式

    首先配置允许访问的用户,采用授权的方式给用户权限 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '123456' WITH GRANT ...

  4. android 当ListView滚动时自动调用 onCheckedChanged 导致CheckBox 状态不停变化 的解决办法

    今天在做一个含有CheckBox 的ListView时,发现当初始化CheckBox的状态后, 滚动ListView,其中CheckBox 的选中状态不停的发生变化.最后发现原因是 ListView滚 ...

  5. Linux 性能监控 : CPU 、Memory 、 IO 、Network

    一.CPU 1.良好状态指标 CPU利用率:User Time <= 70%,System Time <= 35%,User Time + System Time <= 70% 上下 ...

  6. nginx无法启动异常

    Nginx安装过程中可能会报如下错误: /usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx: error while loading ...

  7. Lighttpd1.4.20源代码分析 笔记 状态机之错误处理和连接关闭

    这里所说的错误有两种: 1.http协议规定的错误,如404错误. 2.server执行过程中的错误.如write错误. 对于http协议规定的错误,这里的"错误"是针对clien ...

  8. Canvas文本操作

    Canvas的画图环境提供三个方法如:绘制填充文本:fillText();绘制描边文本:strokeText();绘制文本并返回一个对象:measure();measure()方法返回的对象中包括一个 ...

  9. 跟我学SharePoint 2013视频培训课程——排序、过滤在列表、库中的使用(10)

    课程简介 第10天,SharePoint 2013排序.过滤在列表.库中的使用. 视频 SharePoint 2013 交流群 41032413

  10. APP服务端开发遇到的问题总结(后续再整理解决方法)

    IOS  AES对称加密,加密结果不同,问题解决 IOS http post请求,使用AFNetworing 框架,默认请求content-type为application/json ,所以无法使用@ ...