Description:

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.

实现一个迭代器来遍历二叉查找树。当然首先想到的方法就是中序遍历把有序元素保存在容器中,顺序操作。但是要求uses O(h) memory就不能这样干了。

这里用一个栈来做暂存器,先把所有左节点压栈,这时栈定就一定是最小的元素。之后出栈返回当前的栈顶元素,对于栈顶元素,把以其为顶点的右子树的所有左节点都压栈。以此类推,直到所有元素都遍历一遍。在uses O(h) memory的要求下完成了二叉查找树的遍历。

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator {
public Stack<TreeNode> stack; public BSTIterator(TreeNode root) {
stack = new Stack<TreeNode>();
while(root != null) {
stack.push(root);
root = root.left;
} } /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.empty();
} /** @return the next smallest number */
public int next() { TreeNode node = stack.peek();
int nextVal = node.val;
stack.pop();
TreeNode t = node.right;
while(t != null) {
stack.push(t);
t = t.left;
} return nextVal;
}
} /**
* 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: Binary Search Tree Iterator 解题报告

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

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

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

  3. LeetCode Binary Search Tree Iterator

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

  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 Binary Search Tree Iterator python

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

  6. 【leetcode】Binary Search Tree Iterator

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

  7. 【LeetCode】173. Binary Search Tree Iterator (2 solutions)

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

  8. leetcode-173:Binary Search Tree Iterator(Java)

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

  9. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. Android——数据存储:手机外部存储 SD卡存储

    xml <EditText android:layout_width="match_parent" android:layout_height="wrap_cont ...

  2. DateTime获取一个月的第一天和最后一天

    DateTime dtTarget = DateTime.Now; DateTime FirstDay = dtTarget.AddDays(-DateTime.Now.Day + ); DateTi ...

  3. MVC教程三:URL匹配

    1.使用{parameter}做模糊匹配 {parameter}:花括弧加任意长度的字符串,字符串不能定义成controller和action字母.默认的就是模糊匹配. 例如:{admin}. usi ...

  4. unicode编码转汉字

    String hexB = Integer.toHexString(utfBytes[byteIndex]);   //转换为16进制整型字符串 if (hexB.length() <= 2)  ...

  5. Android代码内存优化建议-Android官方篇

    转自:http://androidperformance.com/ http://developer.android.com/intl/zh-cn/training/displaying-bitmap ...

  6. tensorflow函数解析:Session.run和Tensor.eval的区别

    tensorflow函数解析:Session.run和Tensor.eval 翻译 2017年04月20日 15:05:50 标签: tensorflow / 机器学习 / 深度学习 / python ...

  7. 解决只有单引号的Json格式转换成bean问题

    objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);单引号类似Json格式:{id:124463277,code:null ...

  8. IOC关注服务(或应用程序部件)是如何定义的以及他们应该如何定位他们依赖的其它服务

    IOC关注服务(或应用程序部件)是如何定义的以及他们应该如何定位他们依赖的其它服务.通常,通过一个容器或定位框架来获得定义和定位的分离,容器或定位框架负责: 保存可用服务的集合 提供一种方式将各种部件 ...

  9. MetaSploit Pro 下载地址

    Windows: https://downloads.metasploit.com/data/releases/metasploit-latest-windows-installer.exe Linu ...

  10. Oracle查询优化-使用字符串

    --1.遍历字符串 --1.1.建立测试视图 CREATE OR REPLACE VIEW V AS SELECT '天天向上' AS 汉字,'TTXS' AS 首拼 FROM DUAL; --要求每 ...