【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 ...
随机推荐
- 为什么static数据成员一定要在类外初始化?(转)
1.避免重复定义和初始化 <<c++ primer>>说在类外定义和初始化是保证static成员变量只被定义一次的好方法. 但,为什么static const int就可以在类 ...
- FastText 文本分类使用心得
http://blog.csdn.net/thriving_fcl/article/details/53239856 最近在一个项目里使用了fasttext[1], 这是facebook今年开源的一个 ...
- hadoop集群默认配置和常用配置
http://www.cnblogs.com/ggjucheng/archive/2012/04/17/2454590.html 获取默认配置 配置hadoop,主要是配置core-site.xml, ...
- MonoDB的数据准备
首先是数据的录入,为了分析我们服务器集群的性能,需要准备大量的用户数据,幸运的是mtools提供了mgenerate方法供我们使用.他可以根据一个数据模版向 MongoDB 中插入任意条 json ...
- mycat分库分表 mod-long
转载自:http://blog.csdn.net/sunlihuo/article/details/54574903 下面是配置文件 schema.xml: <?xml version=&quo ...
- iOS9中找不到XXX.dylib 与 is unavailable no availabel on ios (app extension) - use view controller 的解决办法
在 iOS9 中现在找不到 XXX.dylib 了,比如libz.tbd 如果要用到 libz.dylib,可以用下面的办法,来自 Stack Overflow. Go to Build Phase ...
- Unity3d -> Xcode 多个渠道版本发布文件合并
第一步: Users/xxx/.jenkins/jobs/projectname/workspace/build/iOS_iphone 把这里面所有文件拷贝到生成的xcode 工程下的Data目录 如 ...
- Objective-C编程 - 1. 浅谈内存分配
Objective-C语言的对象类型都必须用指针,对象所占的内存是在堆(heap)上分配的. NSString也必须在堆上分配,因此必须用指针. NSString *someString = @&qu ...
- 在线帮助文档编辑器gitbook
https://www.gitbook.com/editor/windows https://yuzeshan.gitbooks.io/gitbook-studying/content/howtous ...
- hibernate和struts实现分页
仓促把功能进行实现一下 类与类之间也没有进行MVC式分开 重要的是逻辑 记录一下 希望其中的些许代码逻辑对你有用 TUserDaoImpl---dao层的实现 package com.pers.h ...