LeetCode(173) Binary Search Tree Iterator
题目
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.
分析
实现二叉查找树的迭代器;
如题目描述,迭代器包括hasNext()和next()两个函数,其中hasNext()函数判断是否还有下一节点,next()函数返回节点元素值;且遍历顺序按照元素递增方式;
我们知道,对于二叉查找树的中序遍历结果为递增有序;所以此题的简单解法即是得到该二叉查找树的中序遍历序列并用queue保存;然后对queue进行处理;
AC代码
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
public:
BSTIterator(TreeNode *root) {
//中序遍历该二叉查找树,得到有序序列
inOrder(root, inOrderNodes);
}
/** @return whether we have a next smallest number */
bool hasNext() {
if (!inOrderNodes.empty())
return true;
return false;
}
/** @return the next smallest number */
int next() {
TreeNode *node = inOrderNodes.front();
inOrderNodes.pop();
return node->val;
}
private:
queue<TreeNode *> inOrderNodes;
void inOrder(TreeNode *root, queue<TreeNode *> &inOrderNodes)
{
if (!root)
return;
if (root->left)
inOrder(root->left, inOrderNodes);
inOrderNodes.push(root);
if (root->right)
inOrder(root->right, inOrderNodes);
}
};
/**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/
LeetCode(173) Binary Search Tree Iterator的更多相关文章
- 【LeetCode 173】Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 【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】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- leetcode-173:Binary Search Tree Iterator(Java)
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(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- 【leetcode】Binary Search Tree Iterator
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 ...
- LeetCode: 669 Trim a Binary Search Tree(easy)
题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...
随机推荐
- ASM 磁盘组的的scrip
之前经常用如下方式进行查询:步骤 1 以oracle用户登录系统.步骤 2 执行如下命令改变ORACLE_SID环境变量.$ export ORACLE_SID=+ASM1[1或者2]需要通过ps - ...
- mysql 存储过程变量及循环的使用
1.用游标循环 BEGIN -- 定义变量 -- 定义done DECLARE done INT; -- 定义 ammeter_id_bl DECLARE ammeter_id_bl DOUBLE; ...
- [USACO07JAN]平衡的阵容Balanced Lineup
[USACO07JAN]平衡的阵容Balanced Lineup 题目描述 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) a ...
- Mybatis find_in_set 子查询,替代 in
1. Mapper文件 2.dao层 3.生成Sql
- 解决Chrome浏览器自动记录用户名和密码的黄色背景问题和该解决方法与tab切换至下一个input冲突的问题。
哈哈哈,是不是标题很长呀,不逗你们了.其实这么长的标题主要就说了两件事: 第一件:解决Chrome浏览器自动记录用户名和密码的黄色背景问题. 第二件:输入完用户名后按下tab键切换至下一个输入密码in ...
- 1126 数字统计 2010年NOIP全国联赛普及组
1126 数字统计 2010年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 请统计某个 ...
- Android 4.4及以后将内容布局延伸到状态栏
首先说明:该文章不是大家说的沉浸式状态栏,网上沉浸式状态栏的博客很多,搜索就有了! 该篇博客的主要目的就是为了将图片显示在状态栏上,让APP看起来更有型!如下图所示: 界面 这个界面的布局就是co ...
- Maven 中maven-assembly-plugin插件的使用笔记 SpringBoot环境
首先创建一个多模块的SpringBoot项目 项目结构 父pom的内容如下: <?xml version="1.0" encoding="UTF-8"?& ...
- svn与git区别简介,git分支操作在mac客户端soureTree和使用命令行如何实现
svn与git区别简介: 性能方面(经过实践的) svn:下载速度慢,因为它其中的源文件太多,并且在show log日志的时候每次都需要去服务器拉取,速度很慢 git:下载速度快,并且git clon ...
- javase基础-Helloword
public class HelloWorld {//创建一个类 :1.类名首字母需要大写:2.类名必须和文件名一致 public static void main(String[] ...