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. IP地址及其子网划分

    说实话,弄到子网划分的时候还是及其头晕的,又是这又是那的,现在我们来讲解一下这些东西, 首先我们来介绍一下IP地址,要弄清子网划分,子网掩码首先还是要弄清IP地址的划分 IP地址是给Internet上 ...

  2. CocoaPods介绍。如何安装Ruby环境,如何下载和安装CocoaPods?

    CocoaPods介绍? 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而其 ...

  3. windows下安装rabbitmq的php扩展amqp

    最近研究rabbitmq队列,linux安装这样的软件一向都是很方便的,但是windows可能会比较麻烦,所以对windows的安装做个记录. windows上使用的php扩展为dll文件,首先去下载 ...

  4. 移动混合开发之HTML5在移动开发中的准则

    1.尽量单页面开发 2.慎重选择前端UI框架,新手最好自己动手. 3.动画特效达到60fps 4.长度单位是用rem,即字体的宽度,字体宽度可根据 window.width/number.

  5. 今天遇到的点击添加按钮button_click代码段无法执行的问题

    首先:本人小白一枚,刚入行,如有表述不当的地方,还请多多指教 网页界面如图: 当点击添加按钮后断点测试进入后台代码运行: 代码会先执行Page_Load页面,当加载完后Page_Load代码会跳转到m ...

  6. Android深度探索--HAL与驱动开发----第三章读书笔记

    1. 什么是Git? Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开 ...

  7. ios手写代码添加控制器

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  8. (spring-第20回【AOP基础篇】)Spring与事务

    要想了解Spring的事务,首先要了解数据库事务的基本知识,数据库并发会产生很多问题,Spring使用ThreadLocal技术来处理这些问题,那么我们必须了解Java的ThreadLocal技术.下 ...

  9. Android开发学习笔记:Intent的简介以及属性的详解【转】

    一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...

  10. GGXX的卡通渲染实现 真的好变态......

    最近在youtube上看了GDC,学了很多东西,最让我震撼的就是ggxx的卡通渲染了.感慨一下,想要用3D做出二次元的效果,真的不容易.现记录一些要点: 1)不要使用normal map来做cel-s ...