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;
}
}
};

最新文章

  1. SpringMVC 表单复选框处理
  2. javascript特效--制作背景电子钟(整点时祝贺生日快乐)
  3. Linux重定向命令
  4. Java String.split()
  5. Bootstrap--全局css样式之图片
  6. c/c++动态分配内存和malloc的使用
  7. MVC+EF 的增删改查操作
  8. DropDownList控件学习
  9. WPF DataGrid 增加&quot;更新&quot;模板列,根据行Row的选择而显示&quot;更新&quot;按钮
  10. Android-它们的定义Notification
  11. SQL server 提示“代理XP”被关闭的解决方法
  12. sfs - django start from scratch
  13. 最强黑吃黑:WEBSHELL大马隐藏万能密码大全
  14. hibernate中Query的list和iterator区别
  15. 修改Jupyter notebook的启动目录
  16. ubuntu搭建 zabbix3.2 with mysql database (Ubuntu 14.04.5 LTS)
  17. How to Create Modifiers Using the API QP_MODIFIERS_PUB.PROCESS_MODIFIERS
  18. 【Android Studio安装部署系列】三十七、从Android Studio3.2升级到Android Studio3.4【以及创建Android Q模拟器】
  19. 【强化学习】用pandas 与 numpy 分别实现 q-learning, saras, saras(lambda)算法
  20. springboot-aop面向切面编程

热门文章

  1. bean工具类
  2. DedeCMS V5.7sp2最新版本parse_str函数SQL注入漏洞
  3. 链栈的c++实现
  4. R语言学习笔记(十三):零碎知识点(36-40)
  5. codeforces 845A Chess Tourney
  6. CERC2017 Gambling Guide,最短路变形,期望dp
  7. ABAP CDS - 字符串函数
  8. Java:static的作用分析
  9. ajax设置自定义头
  10. [网站日志]今天早上遭遇的CPU 100%情况