二叉查找树迭代器 · Binary Search Tree Iterator
[抄题]:
设计实现一个带有下列属性的二叉查找树的迭代器:
- 元素按照递增的顺序被访问(比如中序遍历)
next()和hasNext()的询问操作要求均摊时间复杂度是O(1)
对于下列二叉查找树,使用迭代器进行中序遍历的结果为 [1, 6, 10, 11, 12]
10
/ \
1 11
\ \
6 12
[思维问题]:
[一句话思路]:
有next就全部进入stack并设末尾为空,同时有没有和stack的结果相反。
弹出一个点的同时,next要设置成cur.right,重新入栈。因为next需要往下传。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 先定义一个AddNodeToStack方法,能入栈的先都入栈。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构,为什么不用别的数据结构]:
stack:
左边先进先出,再压右边。
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
整数或其他数据类型的Peeking Iterator,弹出peek
public class BSTIterator {
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode next = null;
void addNodeToStack(TreeNode root) {
while (root != null) {
stack.push(root);
root = root.left;
}
}
public BSTIterator(TreeNode root) {
next = root;
}
/** @return whether we have a next smallest number */
public boolean hasNext() {
if (next != null) {
addNodeToStack(next);
next = null;
}
return !stack.isEmpty();
}
/** @return the next smallest number */
public int next() {
if (! hasNext()) {
return 0;
}
TreeNode cur = stack.pop();
next = cur.right;
return cur.val;
}
}
二叉查找树迭代器 · Binary Search Tree Iterator的更多相关文章
- [Swift]LeetCode173. 二叉搜索树迭代器 | 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(Java)
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】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 ...
- 二叉树前序、中序、后序非递归遍历 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 设计迭代器(搜索树)--------- java
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- Shell 定时发送邮件检查网站脚本/邮件正文
#!/bin/bash ############################################################## # File Name: check_http.s ...
- Quectel module USB driver for linux
The environment settings are as follows: 1. ubuntu 14.04 , linux kernel: linux-lts-xenial-4.4.0 2. m ...
- unity3d工程下的data file作用
projectData文件夹中的data file: 1. Player settings – globalgamemanagers and globalgamemanagers.assets fil ...
- 深度学习中的Normalization模型
Batch Normalization(简称 BN)自从提出之后,因为效果特别好,很快被作为深度学习的标准工具应用在了各种场合.BN 大法虽然好,但是也存在一些局限和问题,诸如当 BatchSize ...
- SQL Server 2016将内置R语言
题记:随着大数据成为一个BuzzWord,和大数据相关的技术也变得越来越火热,其中就包括R语言.而据说SQL Server 2016将会内置R语言支持? R语言作为一个存在很久的语言,在大数据热炒之后 ...
- cecium 笔记
1.Build文件夹 整个拷贝到public文件下,便可使用 2.BingMap(必应地图) Key申请之后,到Build/Cecium/Cecium.js更改默认Key, i.defaultKey ...
- java技术-重点方向
多线程 锁 事务 缓存 hashmap 并发编程
- VB6 MsgBox 函数
在对话框中显示消息,等待用户单击按钮,并返回一个值指示用户单击的按钮. MsgBox(prompt[, buttons][, title][, helpfile, context]) 参数 promp ...
- File 操作
文件乱码 服务器地址 try-with-resource 属性文件获取 文件排序 文件过滤 文件下载 流文件传递 文件乱码: WINDOWS系统桌面默认使用GBK,Linux系统默认使用UTF-8. ...
- mongodb基础学习13-聚集aggregate操作
aggregate可以用的操作与sql的对应关系 下面来看具体操作例子: 分组求和: 求总记录数 商品价格大于50记录分组求和 商品价格大于50且分组记录大于2的分组记录条件 分组库存数,并按库存排序 ...