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)的更多相关文章

  1. 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...

  2. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  3. 【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告

    时间挤挤总是有的 太久不做题,脑子都生锈了.来道水题练练手 题目地址: https://leetcode.com/problems/binary-search-tree-iterator/ 题目内容: ...

  4. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  5. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  6. 【leetcode】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  7. 【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 ...

  8. 【题解】【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 ...

  9. 【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 ...

随机推荐

  1. 弹出层框架layer快速使用

    layer官方及演示文档:layer官方及演示文档 1.将layer整个放入工程内. 2.文件内引入layer.js, <script type="text/javascript&qu ...

  2. Android -- 屏幕亮度

    获取屏幕亮度 int getScreenBrightness(Activity activity) { int value = 0; ContentResolver cr = activity.get ...

  3. 【Scala】Scala-调用Java-集合

    Scala-调用Java-集合 sacla 遍历 java list_百度搜索 13.11 Scala混用Java的集合类调用scala的foreach遍历问题 - 简书

  4. 用LSTM生成武侠人名

    http://magicly.me/2017/04/07/rnn-lstm-generate-name/?utm_source=tuicool&utm_medium=referral 之前翻译 ...

  5. win7下使用Taste实现协同过滤算法

    如果要实现Taste算法,必备的条件是: 1) JDK,使用1.6版本.需要说明一下,因为要基于Eclipse构建,所以在设置path的值之前要先定义JAVA_HOME变量. 2) Maven,使用2 ...

  6. MySql查询时间段的方法(转)

    http://www.jb51.net/article/58668.htm 本文实例讲述了MySql查询时间段的方法.分享给大家供大家参考.具体方法如下: MySql查询时间段的方法未必人人都会,下面 ...

  7. Softmax 函数的特点和作用是什么?

    作者:张欣链接:https://www.zhihu.com/question/23765351/answer/98897364来源:知乎著作权归作者所有,转载请联系作者获得授权. softmax 回归 ...

  8. 模拟日历计算 poj1008

    Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 69932   Accepted: 21524 D ...

  9. String escape/unescape into XML

    Is there any C# function which could be used to escape and un-escape a string, which could be used t ...

  10. (纪录片)统计的乐趣 The Joy of Stats (2010)

    简介: 导演: 丹·希尔曼主演: Hans Rosling类型: 纪录片官方网站: www.bbc.co.uk/programmes/b00wgq0l制片国家/地区: 英国语言: 英语上映日期: 20 ...