Leetcode173. 二叉搜索树迭代器
空间复杂度O(h)而不是O(n),因此不能直接在初始化函数中做中序遍历将结果存储到数组中。
next()和hasNext()时间复杂度为O(1)
首先本题很容易想到用二叉树的中序遍历去解决,外加注意点1.我们得到思路:仅仅将中序遍历最小值之前的节点压入栈中,当next时我们将栈顶元素取出即为最小值返回,当然在此之前需要将下一个最小值找到,并将路径上的所有节点压入栈中以供使用,查看是否迭代到头只需判断栈是否为空即可,如下:
class BSTIterator {
private:
stack<TreeNode*>ss;
public:
BSTIterator(TreeNode* root) {
while(root)
{
while (root)
{
ss.push(root);
root=root->left;
}
}
}
/** @return the next smallest number */
int next() {
TreeNode* cur=ss.top();
int num=cur->val;
ss.pop();
cur=cur->right;
while (cur)
{
ss.push(cur);
cur=cur->left;
}
return num;
}
/** @return whether we have a next smallest number */
bool hasNext() {
return !ss.empty();
}
};
Leetcode173. 二叉搜索树迭代器的更多相关文章
- [Swift]LeetCode173. 二叉搜索树迭代器 | Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- Leetcode173. Binary Search Tree Iterator二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 注意: next() 和hasNext() 操作的时间复杂度是O(1),并 ...
- leetcode_173【二叉搜索树迭代器】
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 示例: BSTIterator iterator = new BSTIte ...
- Leetcode 173. 二叉搜索树迭代器
题目链接 https://leetcode.com/problems/binary-search-tree-iterator/description/ 题目描述 实现一个二叉搜索树迭代器.你将使用二叉 ...
- 173 Binary Search Tree Iterator 二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...
- Java实现 LeetCode 173 二叉搜索树迭代器
173. 二叉搜索树迭代器 实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 示例: BSTIterator iterato ...
- 刷题-力扣-剑指 Offer II 055. 二叉搜索树迭代器
剑指 Offer II 055. 二叉搜索树迭代器 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kTOapQ 著作权归领扣网络所有 ...
- 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 ...
随机推荐
- 图片base64编码解码
1.图片base64编码 https://c.runoob.com/front-end/59 2.图片base64解码 https://www.it399.com/image/base64 https ...
- WPF 精修篇 数据触发器
原文:WPF 精修篇 数据触发器 数据触发器 可以使用Binding 来绑定控件 或者数据源 来触发相关动作 举栗子 <Window.Resources> <Style Target ...
- 洛谷 P5658 括号树
\(50pts\) #include <cstdio> #include <cstring> #include <iostream> #include <al ...
- 大话设计模式Python实现-组合模式
组合模式(Composite Pattern):将对象组合成成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性. 下面是一个组合模式的demo: #!/us ...
- Java实现Mysql的 substring_index 函数功能
Java实现Mysql数据库中 substring_index函数 前言: 由于hive中没有这个 substring_index函数,所以就自定义一个udf函数来调用使用.(不通过hive使用时可以 ...
- Java 泛型示例 - 泛型方法,类,接口
Java Genrics 是 Java 5 中引入的最重要的功能之一. 如果您一直在使用Java Collections并使用版本 5 或更高版本,那么我确定您已经使用过它. Java 中具有集合类的 ...
- 【Linux命令】ldconfig动态链接库管理命令
ldconfig动态链接库管理命令,其目的为了让动态链接库为系统所共享. 作用: 默认搜寻/lilb和/usr/lib,以及配置文件/etc/ld.so.conf内所列的目录下的库文件. 搜索出可共享 ...
- poj-3682 King Arthur's Birthday Celebration
C - King Arthur's Birthday Celebration POJ - 3682 King Arthur is an narcissist who intends to spare ...
- WPF 使用XML作为绑定源时Xaml注意事项
直接在xaml定义时xml时应该注意的! xml数据 <?xml version="1.0" encoding="utf-8"?> <Stri ...
- [算法]LeetCode 152:乘积最大子序列
题目描述: 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4]输出: 6解释: 子数组 [2,3] 有最大乘积 6.示 ...