Design an iterator over a binary search tree with the following properties:

  1. Elements are visited in ascending order (i.e. an inorder traversal)
  2. next() and hasNext() queries run in O(1) time in average.
Example

For the following binary search tree, inorder traversal by using iterator is [1, 6, 10, 11, 12]

10

/     \

1          11

\           \

6           12

Challenge

Extra memory usage O(h), h is the height of the tree.

Super Star: Extra memory usage O(1)
 
Solution: O(h) space.
 /**
* 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的更多相关文章

  1. 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. ...

  2. 【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 ...

  3. [Lintcode]Inorder Successor in Binary Search Tree(DFS)

    题意 略 分析 1.首先要了解到BST的中序遍历是递增序列 2.我们用一个临时节点tmp储存p的中序遍历的下一个节点,如果p->right不存在,那么tmp就是从root到p的路径中大于p-&g ...

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

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

  5. ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java

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

  6. leetcode 173. Binary Search Tree Iterator

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

  7. 【leetcode】Binary Search Tree Iterator(middle)

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

  8. Binary Search Tree Iterator

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

  9. 【leetcode】Binary Search Tree Iterator

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

随机推荐

  1. JavaWeb 学习的第一阶段总结

    本人从事Asp.net开发三年,结合市场情况,综合考虑后决心转向JavaWeb方向.于是开始了自学Java的历程. 首先,我用马士兵的Java基础教学视频,快速地学习了一遍Java基础.因为有C#基础 ...

  2. 用Chrome在电脑上模拟微信浏览器

    1.先了解安卓微信和Ios微信的UA(User agent:用户代理) 安卓微信UA: mozilla/5.0 (linux; u; android 4.1.2; zh-cn; mi-one plus ...

  3. Javascript计算中英文混输字符串长度V2

    同上篇, client端也需要同样规则验证 compact_strlen: function(str) { var strlen = 0; for(var i =0; i<str.length; ...

  4. js里正则表达式详解

    详细内容请点击 正则表达式中的特殊字符 字符 含意 \ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/ ...

  5. javascript触摸事件touch使用

    详细内容请点击 Apple在iOS 2.0中引入了触摸事件API,Android正迎头赶上这一事实标准,缩小差距.最近一个W3C工作组正合力制定这一触摸事件规范.        在本文深入研究iOS和 ...

  6. Javascript常见操作

    图片预加载 var image = new Image();image.onload = onLoad;image.onerror = onLoad;image.src =src; image.com ...

  7. php 图片调整大小 封装类【转载】

    <?php class ImageResize { private $image; private $img_des; private $image_type; private $permiss ...

  8. 什么叫wipe,安卓用户如何去wipe?

    一.wipe是什么意思 wipe从英文单词的字面意思来理解就是:揩,擦;揩干,擦净的意思,从刷机爱好者的专业角度来理解可以认为是一种对手机数据擦除的操作.关于wipe是什么意思比较专业的解答为:wip ...

  9. 集合框架学习之Guava Collection

    开源工具包: Guava : Google Collection Apache:Commons Collecton 1.1 Google Collections Guava:google的工程师利用传 ...

  10. jenkins(一)集成环境搭建示例

    一.环境准备 1.安装java环境 测试自己机器是否已安装,在dos上运行java-version ,出现如下类似结果表示安装完成 2.安装Git/svn git具体配置见我的博客 “GitHub使用 ...