二叉查找树迭代器 · 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 ...
随机推荐
- 解决“Can't bind to local 8630 for debugger”错误--查杀多余进程
Can't bind to local 8630 for debugger 表明本地8630端口被占用 1.Windows平台 在windows命令行窗口下执行: 1.查看所有的端口占用情况 C:\& ...
- 解决php7.1的中遇到的问题
在php7.1中部署微擎遇到问题 1.mysql_xxx函数不支持,修改install.php为mysqli的写法 2.session读取失败,不是php.ini设置的问题,应该是php7.1的bug ...
- Bogart gData.vb
Imports System Imports System.Data Imports System.Data.OleDb Imports Microsoft.VisualBasic Imports S ...
- forward与redirect的区别
1.从地址栏显示来说forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器.浏览器根本不知道服务器发送的内容从哪里来的,所以它的地址 ...
- sed 小结
语法格式1: sed option command file 注意区分 option command, option 以-开始 command 以单?引号包围 ———— 这完全是误会, 我测试好多后发 ...
- 查windows系统开关机记录
https://blog.csdn.net/dufufd/article/details/53316079 者是按下"开始菜单"-"程序"-"管理工具 ...
- 10.Action中的method属性
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 在struts1.x中我们知道通过继承DispatchAction可以实现把 ...
- can't load package the specified module could not be found
can't load package the specified module could not be found 用 Dependency Walker 2.2Dependency Walker ...
- c++builder XE6 线程 tthread
thread TThread class TSleepFunc : public TCppInterfacedObject<TProc> { public: TSleepFunc(TFor ...
- SpringBoot application.yml文件不生效
yml格式对缩进有严格的要求,检查你的yml配置文件是否有不合格的缩进项. 正确的格式如下: server: port: 8881 port前必须有空格, port后的冒号 后面也需要有空格