lintcode-86-二叉查找树迭代器
86-二叉查找树迭代器
设计实现一个带有下列属性的二叉查找树的迭代器:
元素按照递增的顺序被访问(比如中序遍历)
next()和hasNext()的询问操作要求均摊时间复杂度是O(1)样例
对于下列二叉查找树,使用迭代器进行中序遍历的结果为 [1, 6, 10, 11, 12]
挑战
额外空间复杂度是O(h),其中h是这棵树的高度
Super Star:使用O(1)的额外空间复杂度标签
二叉树 二叉查找树 LintCode 版权所有 非递归 谷歌 领英 脸书
方法一(空间复杂度O(n),n为树的节点数):
使用数组保存树的中序遍历的结果
code
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
* Example of iterate a tree:
* BSTIterator iterator = BSTIterator(root);
* while (iterator.hasNext()) {
* TreeNode * node = iterator.next();
* do something for node
*/
class BSTIterator {
public:
vector<TreeNode *> inorder;
int current;
//@param root: The root of binary tree.
BSTIterator(TreeNode *root) {
// write your code here
inorder = inorderTraversal(root);
current = 0;
}
//@return: True if there has next node, or false
bool hasNext() {
// write your code here
if(current == inorder.size()) {
return false;
}
else {
return true;
}
}
//@return: return next node
TreeNode* next() {
// write your code here
return inorder[current++];
}
vector<TreeNode *> inorderTraversal(TreeNode *root) {
vector<TreeNode *> order;
if(root == NULL) {
return vector<TreeNode *>();
}
stack<TreeNode *> s;
TreeNode *p = root;
while(p != NULL || !s.empty()) {
while(p != NULL) {
s.push(p);
p = p->left;
}
if(!s.empty()) {
p = s.top();
order.push_back(p);
s.pop();
p = p->right;
}
}
return order;
}
};
方法二(空间复杂度O(h),h为树的高度):
参考博客http://blog.csdn.net/smile_watermelon/article/details/47280679
code
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
* Example of iterate a tree:
* BSTIterator iterator = BSTIterator(root);
* while (iterator.hasNext()) {
* TreeNode * node = iterator.next();
* do something for node
*/
class BSTIterator {
public:
stack<TreeNode *> inorder;
//@param root: The root of binary tree.
BSTIterator(TreeNode *root) {
// write your code here
putIntoStack(root);
}
//@return: True if there has next node, or false
bool hasNext() {
// write your code here
return !inorder.empty();
}
//@return: return next node
TreeNode* next() {
// write your code here
TreeNode *current = inorder.top();
TreeNode *temp = current;
inorder.pop();
putIntoStack(current->right);
return current;
}
void putIntoStack(TreeNode *root) {
TreeNode *node = root;
while(node != NULL) {
inorder.push(node);
node = node->left;
}
}
};
lintcode-86-二叉查找树迭代器的更多相关文章
- LintCode 11 二叉查找树的搜索区间
题目链接:http://www.lintcode.com/zh-cn/problem/search-range-in-binary-search-tree/ 1.描述 给定两个值 k1 和 k2(k1 ...
- 二叉查找树迭代器 · Binary Search Tree Iterator
[抄题]: 设计实现一个带有下列属性的二叉查找树的迭代器: 元素按照递增的顺序被访问(比如中序遍历) next()和hasNext()的询问操作要求均摊时间复杂度是O(1) 对于下列二叉查找树,使用迭 ...
- Java for LintCode 验证二叉查找树
给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值. 节点的右子树中的值要严格大于该节点的值. 左右子树也必须是二叉查找树. ...
- lintcode:在二叉查找树中插入节点
题目: 在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树. 样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样 ...
- dfs 二叉树中序遍历迭代解法——求解BST中第k小元素
BST中第K小的元素 中文English 给一棵二叉搜索树,写一个 KthSmallest 函数来找到其中第 K 小的元素. Example 样例 1: 输入:{1,#,2},2 输出:2 解释: 1 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- 编写TreeSet类的实现程序,其中相关的迭代器使用二叉查找树
package com.test.tree; import java.util.Iterator; /** * 编写TreeSet类的实现程序,其中相关的迭代器使用二叉查找树. * 在每个节点上添加一 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- [LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- lintcode:验证二叉查找树
题目 给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值. 节点的右子树中的值要严格大于该节点的值. 左右子树也必须是二叉查找树. 一个 ...
随机推荐
- Oracle 用户、授权、角色管理
Oracle 用户管理 一.创建用户的Profile文件SQL> create profile student limit // student为资源文件名FAILED_LOGIN_ATTEMP ...
- jsp页面的传值(popup)
jsp页面与xml文件对应的关系: 例:网页上jsp的url为----purchase_app_btn.do? 对应xml文件下的 <action path="/purchase_ap ...
- 序列(Sequence)创建、使用、修改和删除
序列(Sequence)是用来生成连续的整数数据的对象.序列常常用来作为主键中增长列,序列中的可以升序生成,也可以降序生成. 语法结构:创建序列 CREATE SEQUENCE sequence_na ...
- Hibernate知识点小结汇总
Hibernate部分 1.为什么要使用Hibernate开发你的项目呢?Hibernate的开发流程是怎么样的? 为什么要使用 ①.对JDBC访问数据库的代码做了封装,大大简化了数据访问层繁琐的重复 ...
- JDBC编程:使用 Statement 修改数据库
获取数据连接后,即可对数据库中的数据进行修改和查看.使用 Statement 接口可以对数据库中的数据进行修改,下面是程序演示. /** * 获取数据库连接,并使用SQL语句,向数据库中插入记录 */ ...
- 小程序swiper不显示图片
按照文档上的代码运行后,发现图片不显示 解决办法: app.wxss文件 align-items: center;这句话删除了,运行 OK!
- maven项目打包后war文件丢失配置文件
使用maven package打包项目时出现配置文件丢失的现象,此类问题解决办法如下: 在web项目pom.xml 文件中添加如下: 在<build>标签中添加如下配置: <reso ...
- 微信小程序引用iconfont图标字体解决方案;
1)首先,登录阿里巴巴iconfont.cn 2)新建项目 3)点击icon收藏 4)加入到test项目中 5)下载到本地解压 6)生成代码 7)复制iconfont.css到xxx.wx ...
- apache使用.htaccess文件中RewriteRule重定向后,URL中的加号无法解析
今天在使用.htaccess做伪静态的时候,发生一件怪事,URL里存在C++时会有问题,在处理C++这个词的时候,无论如何,$_GET都得不到++,只能得到C空格. 一开始我以为是没用urlencod ...
- CentOS 同步时间的方法
与时间服务器上的时间同步的方法 1. 安装ntpdate工具 # yum -y install ntp ntpdate 2. 设置系统时间与网络时间同步 # ntpdate cn.pool.ntp ...