【Binary Search Tree Iterator 】cpp
题目:
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 {
private:
vector<TreeNode*> list;
int index;
public:
BSTIterator(TreeNode *root) {
BSTIterator::treeToList(root, this->list);
this->index = ;
} static void treeToList(TreeNode* root, vector<TreeNode*>& ret)
{
if ( !root ) return;
BSTIterator::treeToList(root->left, ret);
ret.push_back(root);
BSTIterator::treeToList(root->right, ret);
} /** @return whether we have a next smallest number */
bool hasNext() {
return index < this->list.size();
} /** @return the next smallest number */
int next() {
return list[index++]->val;
}
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/
tips:
这个题目的核心就是在于中序遍历 把BST转化成vector,这样可以实现题目的要求。
【Binary Search Tree Iterator 】cpp的更多相关文章
- 【leetcode】Binary Search Tree Iterator
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 【LeetCode】173. Binary Search Tree Iterator (2 solutions)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- leetcode-173:Binary Search Tree Iterator(Java)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- LeetCode: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 【leetcode】Binary Search Tree Iterator(middle)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 【LeetCode 173】Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告
时间挤挤总是有的 太久不做题,脑子都生锈了.来道水题练练手 题目地址: https://leetcode.com/problems/binary-search-tree-iterator/ 题目内容: ...
- 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...
随机推荐
- 利用PCHunter结束各种进程
http://www.epoolsoft.com/ 经测试,可结束主动防御等.
- 利用批处理结合Msbuild实现快速编译
我们经常在用vs2005做项目的时候会把一个项目分成几个模块(不管是对于功能上,还是系统构架上面),为的是以后部署,还有修改维护时候的方便.这样就会带来一个问题,随着模块的增加(这里所说得每个模块就是 ...
- POJ-1840 Eqs---二分
题目链接: https://vjudge.net/problem/POJ-1840 题目大意: 给出一个5元3次方程,输入其5个系数,求它的解的个数 其中系数 ai∈[-50,50] 自变量xi∈[ ...
- 【CCPC-Wannafly Winter Camp Day4 (Div1) F】小小马(分类讨论)
点此看题面 大致题意: 给你一张\(n*m\)的棋盘,问你一匹马在两个点中是否存在一条经过黑白格子数目相等的路径. 简化题目 首先,我们来简化一下题目. 考虑到马每次走的时候,所经过的格子的颜色必然发 ...
- HDU(1698),线段树区间更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 区间更新重点在于懒惰标记. 当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r- ...
- 缓存验证Last-Modifie和Etag的使用
看这张图,是浏览器发出请求到请求缓存到过程,这么一个原理 那么http里面如何进行验证呢?主要有两个验证到http头 Last-Modified(上次修改时间) 主要配合If-Modified-Sin ...
- vuejs样式绑定
第一种:class的对象绑定,class引用的是一个对象,这个对象的属性显示不显示由变量决定 <style> .activated{ color:red; } </style> ...
- MongoDB在MFC下使用C++驱动编译错误的解决
.post p{text-indent: 2em;} 今天使用MongoDB的C++驱动,在编译连接的时候一直出现错误,显示的string_data.h下93行max宏的问题,可视其本身并不是调用ma ...
- (jdbc和cmd)sqlite数据迁入mysql(导入导出)
从sqlite进行导出数据 进行cmd命令 第一步:sqlite3->.open [文件路径](打开连接)->.tables(这个是查看表是否有没有)->.cd [切换的盘符](这里 ...
- powerdesigner15 反向工程