✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java
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的更多相关文章
- [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- leetcode 173. Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...
- 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 ...
- 173 Binary Search Tree Iterator 二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 【LeetCode】173. Binary Search Tree Iterator (2 solutions)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 【leetcode】Binary Search Tree Iterator
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- 转载《遭受arp攻击怎么办》
ARP(Address Resolution Protocol,地址解析协议)协议的基本功能就是通过目标设备的IP地址,查询目标设备的MAC地址,以保证通信的进行.ARP攻击仅能在以太网(局 域网如: ...
- SQL 常用函数
--将字符串中从某个字符开始截取一段字符,然后将另外一个字符串插入此处 select stuff('hi,world!',4,4,'****') --返回值hel****orld! --返回从指定 ...
- pyinstaller 用法
参考:http://pythonhosted.org/PyInstaller/#installing-pyinstaller 1.下载pyinstaller和PyWin32 目前pyinstalle ...
- is a 、like a、has a
has a 代表的是对象和它的成员的从属关系.可以分为组合与聚合两种. 组合:表示两个对象是整体与部分的关系为强关系 聚合:表示两个对象是整体与部分的关系为弱关系 is a 它是类继承关系,只是覆盖了 ...
- c语言计算矩阵特征值和特征向量-1(幂法)
#include <stdio.h> #include <math.h> #include <stdlib.h> #define M 3 //方阵的行数 列数 #d ...
- Java泛型中的? super T语法
? super T 语法将泛型类限制为所有T的超类(包括T自身),但只能用于参数中,不可以在返回值用加以限定.如果不加以限定,假设某个函数头为? super Manager get()由于编译器不知道 ...
- 涵涵和爸爸习惯养成进度表(一)(May 5 - May 25)
规则说明 三周时间(21天)内,没有哭脸,不超过三个无表情脸,可以给一个奖励(动画书等) 涵涵违反规则,在爸爸和妈妈都同意的情况下,可以给无表情脸 爸爸违反规则,在妈妈和涵涵都同意的情况下,可以给无表 ...
- mount: /dev/sdb1 already mounted or /mnt/hdb busy 导致NameNode无法启动
最近,公司由于断电导致的服务器关机,等到来电了,重启集群发现Namenode无法启动,查看原因是由于无法加在faimage文件,在查看一下Namenode的磁盘挂在情况(df -h命令查看),发现磁盘 ...
- jquery实现css3动画
jquery animate改变元素样式时,只支持数字值的变化,比如width,height等,但是css3属性状态值很多都不是数字值,而是字符串和数字混合在一起,比如translate(), rot ...
- 内存对齐 和 sizeof小结
数据对齐(内存对齐)指该数据所在的地址必须是该数据长度的整数倍.X86CPU能直接访问对齐的数据,当它试图访问未对齐的数据时,会在内部进行一系列的调整,降低运行速度.数据对齐一般出现在结构体和类中,在 ...