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. winrar5.50去广告教程(仅供学习使用)

    https://blog.csdn.net/EnigmCode/article/details/78328873 第一步:到WinRAR官网www.rarlab.com下载自己需要的版本,我这里选择C ...

  2. Java奇淫巧技之Lombok

    http://blog.csdn.net/ghsau/article/details/52334762

  3. Dll 导出函数那些破事

    经常使用VC6的Dependency查看DLL导出函数的名字,会发现有DLL导出函数的名字有时大不相同,导致不同的原因大多是和编译DLL时候指定DLL导出函数的界定符有关系. VC++支持两种语言:即 ...

  4. Lucene的查询语法,JavaCC及QueryParser(1)

    http://www.cnblogs.com/forfuture1978/archive/2010/05/08/1730200.html 一.Lucene的查询语法 Lucene所支持的查询语法可见h ...

  5. system函数的应用一例

    system函数的应用一例

  6. 命令行下的html转pdf工具wkhtmltopdf

    基于webkit和qt的html转pdf的命令行工具,非常好使 http://code.google.com/p/wkhtmltopdf/ http://www.cnblogs.com/shanyou ...

  7. POJ 3009:Curling 2.0 推箱子

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14090   Accepted: 5887 Desc ...

  8. 小议IE10下的DrawToBitmap方法

    在完成博文“PS网页设计教程XXIV——从头设计一个漂亮的网站”后. 出于习惯,打开之前“利用Webbrowser类实现超长网页的截屏的实现(解决报错不能截取的难题)”中的代码的程序,截取博文作为资料 ...

  9. Springmvc 服务器端文件下载

    转自:http://blog.csdn.net/boneix/article/details/51303280 业务场景:点击下载后直接保存而不是打开 解决代码:前端传入url /** * 返回流 * ...

  10. iOS开发技巧 - Size Class与iOS 8多屏幕适配(一)

    0. 背景: 在iOS开发中,Auto Layout(自动布局)能解决大部分的屏幕适配问题. 但是当iPhone 6和iPhone 6 Plus发布以后, Auto Layout已经不能解决复杂的屏幕 ...