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.

设计一个二叉搜索树的迭代器。要求其中的next()与hasNext()是平均O(1)的时间复杂度,O(h)的空间复杂度,h是树高。

1、用栈来实现,栈中存储的是当前路径的左孩子。

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator { Stack<TreeNode> stack;
public BSTIterator(TreeNode root) {
stack = new Stack();
if (root == null){
return ;
}
while (root != null){
stack.push(root);
root = root.left;
}
} /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.isEmpty();
} /** @return the next smallest number */
public int next() {
TreeNode node = stack.pop();
int ans = node.val;
if (node.right != null){
node = node.right;
while (node != null){
stack.push(node);
node = node.left;
}
}
return ans;
} } /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/

2、用list实现。直接排序然后存储在list中,代码简单高效。(参考discuss)。

这种方法虽然比上面的方法快并且简单,但是使用的空间是O(N)的空间,比上一个多,如果上一个题意中说明该设计类只能用O(h)的空间,那么这种解法就不对了。

ArrayDeque<Integer> list;

public BSTIterator(TreeNode root) {
list = new ArrayDeque<Integer>();
inorderTraverse(root);
} void inorderTraverse(TreeNode root)
{
if(root == null)
return;
inorderTraverse(root.left);
list.addLast(root.val);
inorderTraverse(root.right);
} /** @return whether we have a next smallest number */
public boolean hasNext() {
if(list.isEmpty())
return false;
else
return true;
} /** @return the next smallest number */
public int next() {
return list.removeFirst();
}

✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java的更多相关文章

  1. [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器

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

  2. leetcode 173. Binary Search Tree Iterator

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

  3. leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)

    https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...

  4. Java for LeetCode 173 Binary Search Tree Iterator

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

  5. 173 Binary Search Tree Iterator 二叉搜索树迭代器

    实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...

  6. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

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

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

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

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

随机推荐

  1. JSP内置对象---用户登录页面(get和post)

    Login.jsp 页面: <%@ page language="java" import="java.util.*" contentType=" ...

  2. MiniTwitter记住密码等功能实现

    一.SharedPreferences的用法:(相关实现功能的只是了解) 由于SharedPreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力.但它是通过其Editor接 ...

  3. android editText 监听事件

    在软键盘中注意 在监听的 edittext中 使用android:imeOptions属性的时候,一定要对EditText设置 android:inputType 或者 设置 android:sing ...

  4. 百度前端技术学院2015JavaScript基础部分实现自己的小型jQuery

    // 实现一个简单的Query function $(selector) { ); if (firstChar == "#") { var len = selector.split ...

  5. Android sdk环境配置

      1.环境搭建 1.1.JDK安装 1.2.Eclipse安装 1.3.Android SDK安装 1.4.ADT安装 1.5.创建AVD 详细细节 http://www.cnblogs.com/s ...

  6. impress.js webslide 参数

    data-transition-duration="2000" 改变切换场景的速度,默认1000data-perspective="500" 改变透视的深度,默 ...

  7. Mvc api HelpPage 与注释

    一.添加包Microsoft.AspNet.WebApi.HelpPage可以自动给api生成帮助页面,url:/help 二.help加注释: 1. 2. public static class H ...

  8. NLog的使用

    1.项目添加NLog的项目引用: 2.将工程的framework框架改为3.5或4.0,将配置文件[NLog.config]拷贝至运行的bin/(release/debug)路径下 3.项目中代码使用 ...

  9. Java集合与算法

    梗概: 集合接口 集合实现(链表.数组列表.散列集.树集.队列与双端队列.映射表) 集合与数组之间的转换 算法(排序.二分查找). 文章链接: http://mp.weixin.qq.com/s?__ ...

  10. linux下解压war文件命令

    jar -xvf project.war -->解压到当前目录下. -f  指定 JAR 文件名,通常这个参数是必须的 -v  显示过程信息