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-二叉查找树迭代器的更多相关文章

  1. LintCode 11 二叉查找树的搜索区间

    题目链接:http://www.lintcode.com/zh-cn/problem/search-range-in-binary-search-tree/ 1.描述 给定两个值 k1 和 k2(k1 ...

  2. 二叉查找树迭代器 · Binary Search Tree Iterator

    [抄题]: 设计实现一个带有下列属性的二叉查找树的迭代器: 元素按照递增的顺序被访问(比如中序遍历) next()和hasNext()的询问操作要求均摊时间复杂度是O(1) 对于下列二叉查找树,使用迭 ...

  3. Java for LintCode 验证二叉查找树

    给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值.    节点的右子树中的值要严格大于该节点的值.    左右子树也必须是二叉查找树. ...

  4. lintcode:在二叉查找树中插入节点

    题目:  在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树.  样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样 ...

  5. dfs 二叉树中序遍历迭代解法——求解BST中第k小元素

    BST中第K小的元素 中文English 给一棵二叉搜索树,写一个 KthSmallest 函数来找到其中第 K 小的元素. Example 样例 1: 输入:{1,#,2},2 输出:2 解释: 1 ...

  6. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  7. 编写TreeSet类的实现程序,其中相关的迭代器使用二叉查找树

    package com.test.tree; import java.util.Iterator; /** * 编写TreeSet类的实现程序,其中相关的迭代器使用二叉查找树. * 在每个节点上添加一 ...

  8. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  9. [LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  10. lintcode:验证二叉查找树

    题目 给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值. 节点的右子树中的值要严格大于该节点的值. 左右子树也必须是二叉查找树. 一个 ...

随机推荐

  1. 【PTA 天梯赛训练】修理牧场(哈夫曼树+优先队列)

    农夫要修理牧场的一段栅栏,他测量了栅栏,发现需要N块木头,每块木头长度为整数L​i​​个长度单位,于是他购买了一条很长的.能锯成N块的木头,即该木头的长度是L​i​​的总和. 但是农夫自己没有锯子,请 ...

  2. SAP存储税率税码的相关表

    T007A 稅關鍵字 T007B 會計中的稅務處理 T007C Groupings for tax codes T007D 賦稅群組的內文 T007F 銷項 / 進項群組的稅 T007G 不再使用該表 ...

  3. 【php学习-4】

    var_1=11; print $var1->var_1.$var2->var_1; ?> print_Color(); ?> y=self::$x; # code... } ...

  4. npm install 报错

    今天准备在服务器上部署一下pm2,发现 npm install -g pm2   爆出了错误 error Unexpected end of JSON input while parsing near ...

  5. Mysql通过Adjacency List(邻接表)存储树形结构

    转载自:https://www.jb51.net/article/130222.htm 以下内容给大家介绍了MYSQL通过Adjacency List (邻接表)来存储树形结构的过程介绍和解决办法,并 ...

  6. UVA ~ 514 ~ Rails (栈)

    参考:https://blog.csdn.net/ZscDst/article/details/80266639 #include <iostream> #include <cstd ...

  7. 生産管理(PP)

    伝票系 製造指図 マスタ系 生産資源/治工具 作業区 能力 作業手順 作業バージョン 作業記録 需要予測プロファイル 計画手配 MRP レシピ その他 カスタマイズ系 BOM関連 製造指図確認 伝票系 ...

  8. 成都Uber优步司机奖励政策(3月25日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 常用操作提高效率 之 for 与in

    问题如何而来: 对于刚参加工作的我  批量删除数据通常采用的是前端传递到后台一个对象的id字符串  通过逗号分隔的多个id  或者收的直接是一个id数组 两个原理一样第一个后台要在次使用split(& ...

  10. C#获取网络图片

    简单获取图片 string url = zhi_txt.Text;//图片地址 string dizhi = lujing.Text;//图片下载后保存路径及图片名称要写在一块 WebClient w ...