【LeetCode】173. Binary Search Tree Iterator (2 solutions)
Binary Search Tree Iterator
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
解法一:暴力解法先不考虑空间复杂度
中序遍历后装入队列,顺序输出。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
public:
queue<int> minq; map<TreeNode*, bool> m;
stack<TreeNode *> s;
BSTIterator(TreeNode *root) {
//inOrder traversal
if(root != NULL)
{
s.push(root);
m[root] = true;
while(!s.empty())
{
TreeNode* top = s.top();
if(top->left && m.find(top->left) == m.end())
{
s.push(top->left);
m[top->left] = true;
continue;
}
minq.push(top->val);
s.pop();
if(top->right && m.find(top->right) == m.end())
{
s.push(top->right);
m[top->right] = true;
}
}
}
} /** @return whether we have a next smallest number */
bool hasNext() {
return !minq.empty();
} /** @return the next smallest number */
int next() {
int front = minq.front();
minq.pop();
return front;
}
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/
解法二:空间复杂度O(h)的解法
每次取出栈顶元素(即当前最小)后,查找下一个元素并压栈。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
public:
stack<TreeNode*> stk;
int nextmin;
BSTIterator(TreeNode *root) {
while(root)
{
stk.push(root);
root = root->left;
}
} /** @return whether we have a next smallest number */
bool hasNext() {
if(!stk.empty())
{
TreeNode* top = stk.top();
stk.pop();
nextmin = top->val;
TreeNode* cur = top->right;
if(cur)
{
stk.push(cur);
cur = cur->left;
while(cur)
{
stk.push(cur);
cur = cur->left;
}
}
return true;
}
else
return false;
} /** @return the next smallest number */
int next() {
return nextmin;
}
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/
【LeetCode】173. Binary Search Tree Iterator (2 solutions)的更多相关文章
- 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告
时间挤挤总是有的 太久不做题,脑子都生锈了.来道水题练练手 题目地址: https://leetcode.com/problems/binary-search-tree-iterator/ 题目内容: ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- 【leetcode】Validate Binary Search Tree(middle)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【题解】【BST】【Leetcode】Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【leetcode】 Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
随机推荐
- Java中夏令时带来的Date不一致问题 (转)
http://www.cnblogs.com/snake-hand/archive/2013/06/10/3131157.html 最近同事W发现使用Java Date创建日期,在不同的机器上执行,得 ...
- MySQL在线大表DDL操作 (转)
http://www.cnblogs.com/janehoo/p/5382474.html 线大表DDL操作的方法: 1.主从架构轮询修改 需要注意: a.主库会话级别的记录binglog的参数关闭 ...
- 【转】字符编码笔记:ASCII,Unicode 和 UTF-8
原文:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html https://www.key-shortcut.com/ ...
- Spring(十一):Spring配置Bean(四)SpEL
Spring表达式语言:SpEL 1)Spring表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. 2)语法类似于EL:SpEL使用#{...}作为界定符,所有在大框号 ...
- Mockito单测,mock service层的mapper
转载:https://blog.csdn.net/paincupid/article/details/53561435 1.引入mockito jar包 <dependency> < ...
- 【Python】使用torrentParser1.02对多文件torrent的分析结果
C:\Users\horn1\Desktop\python\41-torrentParser>python torrentParser.py 文件名=./6.torrent 文件结构: anno ...
- iOS应用程序状态图
理解应用的状态对于我们开发iOS大有裨益. 当前应用所处什么状态,什么促使它在各个状态间进行过渡,你的代码又是如何 唤醒这些过渡,等等等等. 先请看下图: 1. 当应用出于非运行状态时,它处于图中的& ...
- JavaScript 时间、格式、转换及Date对象总结
悲剧的遇到问题,从前台得到时间,“Tue Jan 29 16:13:11 UTC+0800 2008”这种格式的,想再后台解析成想要的格式,但是在后台就是解析不了SimpleDateFormat也试着 ...
- webpack4 css 文件提取 压缩 MiniCssExtractPlugin optimize-css-assets-webpack-plugin
1.使用的插件 MiniCssExtractPlugin:https://webpack.js.org/plugins/mini-css-extract-plugin/#src/components/ ...
- Influxdb数据存储
环境: CentOS6.5_x64 InfluxDB版本:1.1.0 InfluxDB存储引擎看起来很像一个LSM Tree,它包含预写日志和类似存储在LSM Tree中的SSTables只读数据. ...