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. Spring Boot 更换 Banner

    Spring 启动时,会有一个Banner图案,这个图案是可以更换的 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ ...

  2. Android——SQLite数据库(一)创建数据库、创建表、初始化数据

    xml <Button android:layout_width="match_parent" android:layout_height="wrap_conten ...

  3. 轻松使用jquery解析XML

    xml文件结构:books.xml <?xml version="1.0" encoding="UTF-8"?><root>    &l ...

  4. spark学习系列

    转自: http://www.cnblogs.com/magj2006/p/4316264.html spark 系列文章汇总 源码导读 spark 源码导读1 从spark启动脚本开始 spark ...

  5. 最小顶点覆盖(Minimum Vertex Cover)与最大独立集(Maximum Independent Set)

    问题描述:就是在图中找最小的点集,使得覆盖所有边. 和独立集等价:独立集问题:在图中找最大的点集,使得点集内的所有点互不相连. 引理:顶点覆盖集和独立集互补. 上面这个引理使得这两个问题可以相互规约, ...

  6. 【转】OPenGL MFC绘图

    一.简介 GDI是通过设备句柄(Device Context以下简称"DC")来绘图,而OpenGL则需要绘制环境(Rendering Context,以下简称"RC&q ...

  7. e637. 向剪切板获取和粘贴文本

    This examples defines methods for getting and setting text on the system clipboard. // If a string i ...

  8. 用C结构体来实现面向对象编程,ti xDAIS标准算法就这么搞的(1)

    用C结构体来实现面向对象编程,ti xDAIS标准算法就这么搞的. 测试代码如下: #include <stdio.h> #include <stdlib.h> #includ ...

  9. eclipse下载,安装,JDk环境配置教程(多图)

    第一步:下载eclipse,并安装. 下载链接:http://www.eclipse.org/downloads/ 点击 Download Packages; 根据自己的系统选择32位还是64位的,点 ...

  10. mysql 从sql存储文件恢复数据库乱码

    场景一: 一台电脑上导出的sql文件到另一台电脑上恢复数据库,汉字全部是乱码,然后可能还有部分数据提示超长. 场景二: 拿到的sql文件不是原始的导出sql文件,只有表结构和表数据,出现的问题和场景一 ...