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. Builder模式

    原文来源于http://www.iteye.com/topic/71175 对于Builder模式很简单,但是一直想不明白为什么要这么设计,为什么要向builder要Product而不是向知道建造过程 ...

  2. mac idea中 maven项目添加的时候没有java文件

    file --other setting --maven  选中第二项即可  apply下

  3. c语言计算矩阵特征值和特征向量-1(幂法)

    #include <stdio.h> #include <math.h> #include <stdlib.h> #define M 3 //方阵的行数 列数 #d ...

  4. Apple Pay的快速实现

    一.在Apple开发者中心配置 AppleID 和 Merchant IDs 二.配置好证书后在Xcode中开启Apple Pay 三.代码实现 3.1 判断是否支持Apple Pay,如果支持又将支 ...

  5. 涵涵和爸爸习惯养成进度表(二)(May 30 - )

    规则说明 22天内,没有哭脸,不超过三个无表情脸,可以给一个奖励(动画书等) 涵涵违反规则,在爸爸和妈妈都同意的情况下,可以给无表情脸 爸爸违反规则,在妈妈和涵涵都同意的情况下,可以给无表情脸 获奖记 ...

  6. 移动测试主要使用的测试框架,基于python

    1.uiautomator google自己的测试框架,可以跨应用测试, 语言支持java,python也可以在GitHub上找到封装的包,去调用uiautomator. 2.Robotium jav ...

  7. linux内核分析——扒开系统调用的三层皮(上)

    20135125陈智威 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 系统调用 ...

  8. Windows集成认证全过程

    开发环境:win7旗舰版,vs2013 服务器环境:windows server 2008 R2 IIS7.5 目的:在局域网搭建一个网站,用户必须使用域帐号登录网站访问 Step 1: 创建proj ...

  9. cell 内部 设置width 总不对

    今天 在Cell 里设置屏幕宽 如果 在layoutSubviews 使用 self.width(自己写的分类) 或者 self.view.size.width  都可以,这里 4 4s 5 5s 都 ...

  10. UICollectionview实现自定义cell的移动删除

    今天 ,有群里人询问了 ,支付宝首页的UICollectionview 的cell(其实不能成为cell,应该是item,不过大家习惯这么称呼了)怎么实现 自定义的拖拽 和删除,然后我查了下资料,它的 ...