【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 ...
随机推荐
- 解决robotframework安装时提示wxPython not found问题
背景:想把现在pc的项目做成关键字的形式,可以让功能测试人员也参与到自动化测试中,于是就找到robotframework这个框架,试用下怎么样,在安装时就遇到很多问题,安装的帖子有很多,很详细,如:h ...
- 转:spring mvc返回json数据格式
转:http://www.cnblogs.com/ssslinppp/p/4675495.html <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: ...
- 2017.10.1 JDBC数据库访问技术
4.1 JDBC技术简介 4.1.1 定义 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的 java API,由一组类与接口组成,通过 ...
- kiiti分割的数据及其处理
kitti和cityscape的gt的分割不太一样,下边缘不再是从黑色开始的,而是直接是类别 red,green,blue = img_gt[i,j] 1.道路的颜色(紫色):128 64 128 2 ...
- splay版
指针是个好东西 不过就是得判空 还有别忘传引用(其实应该都传引用) #include<cstdio> #include<algorithm> #include<iostr ...
- Java面试不得不知的问题(一)
程序员面试 1. 面向对象的特征有哪些方面 · 抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...
- 由fastRPC产生的DB服务
根据整理的RPC模型,在此上,根据最近的项目,发布了DB服务,操作数据库.以RPC模型,发布数据库的操作服务,主要发送SQL语句,在服务端执行:同时引入了流行的数据库连接池:服务端还发布了文件接收服务 ...
- LeetCode297. Serialize and Deserialize Binary Tree
题目 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据. 请设计一个算法来实 ...
- mysql架构和历史
存储引擎 查看: show table status like 'bigcourse'; 结果: +-----------+--------+---------+------------+------ ...
- 学习Pytbon第十天 函数2 内置方法和匿名函数
print( all([1,-5,3]) )#如果可迭代对象里所有元素都为真则返回真.0不为真print( any([1,2]) )#如果数据里面任意一个数据为真返回则为真a= ascii([1,2, ...