leetcode_173【二叉搜索树迭代器】
实现一个二叉搜索树迭代器。你将使用二叉搜索树的根节点初始化迭代器。
调用 next()
将返回二叉搜索树中的下一个最小的数。
示例:
- BSTIterator iterator = new BSTIterator(root);
- iterator.next(); // 返回 3
- iterator.next(); // 返回 7
- iterator.hasNext(); // 返回 true
- iterator.next(); // 返回 9
- iterator.hasNext(); // 返回 true
- iterator.next(); // 返回 15
- iterator.hasNext(); // 返回 true
- iterator.next(); // 返回 20
- iterator.hasNext(); // 返回 false
提示:
next()
和hasNext()
操作的时间复杂度是 O(1),并使用 O(h) 内存,其中 h 是树的高度。- 你可以假设
next()
调用总是有效的,也就是说,当调用next()
时,BST 中至少存在一个下一个最小的数。
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class BSTIterator {
- public:
- BSTIterator(TreeNode* root) {
- //初始化所有左节点入栈
- for(; root != NULL; root = root->left){
- stk.push(root);
- }
- }
- /** @return the next smallest number */
- int next() {
- TreeNode* pnode = stk.top();
- stk.pop();
- int val = pnode->val;
- //换成右子树的根
- pnode = pnode->right;
- //右子树的所有左节点入栈
- for(; pnode != NULL; pnode = pnode->left){
- stk.push(pnode);
- }
- return val;
- }
- /** @return whether we have a next smallest number */
- bool hasNext() {
- return stk.empty()? false: true;
- }
- private:
- stack<TreeNode*> stk;
- };
- /**
- * Your BSTIterator object will be instantiated and called as such:
- * BSTIterator* obj = new BSTIterator(root);
- * int param_1 = obj->next();
- * bool param_2 = obj->hasNext();
- */
leetcode_173【二叉搜索树迭代器】的更多相关文章
- [Swift]LeetCode173. 二叉搜索树迭代器 | Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- Leetcode 173. 二叉搜索树迭代器
题目链接 https://leetcode.com/problems/binary-search-tree-iterator/description/ 题目描述 实现一个二叉搜索树迭代器.你将使用二叉 ...
- 173 Binary Search Tree Iterator 二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...
- Leetcode173. Binary Search Tree Iterator二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 注意: next() 和hasNext() 操作的时间复杂度是O(1),并 ...
- Java实现 LeetCode 173 二叉搜索树迭代器
173. 二叉搜索树迭代器 实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 示例: BSTIterator iterato ...
- 刷题-力扣-剑指 Offer II 055. 二叉搜索树迭代器
剑指 Offer II 055. 二叉搜索树迭代器 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kTOapQ 著作权归领扣网络所有 ...
- LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] 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 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- 【题解】 [NOI1999]生日蛋糕
题面 传送门 Solution 搜索每一层的半径和高度,然后加入一些剪枝就好了. #include<stdio.h> #include<stdlib.h> #include&l ...
- GPU大百科全书 第一章:美女 方程与几何
沉鱼落雁 前言:当你酣战在星际2的时候,或者当你在艾泽拉斯游历的时候,你有没有想过,眼前的这些绚丽的画面究竟是怎么来的呢?也许对大多数人来说,GPU对于图形的处理过程并不是那么重要,但总会有些人, ...
- [Flex] 修改注释中的@author方法
当然,在Flash Builder里,按Ctrl+Shift+D可以很方便在添加AsDoc注释,也可以修改,可是有些生成的@author是系统的用户名(如:administor),如何修改 修改方法之 ...
- react的一些思考
在做好第一个需求之后,我接到了一个react写的产品,这让我异常的兴奋,终于能写react了 开始做的时候整体框架已经搭建好了,这让我有点小失落,我还以为我要开始搭框架了呢,没事,搭的也挺好的. 有了 ...
- 10分钟教你用VS2017将代码上传到GitHub
前言 关于微软的Visual Studio系列,真可谓是宇宙最强IDE了.不过,像小编这样的菜鸟级别也用不到几个功能.今天给大家介绍一个比较实用的功能吧,把Visual Studio 2017里面写好 ...
- js 实现全国省市区三级联动
效果: index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /& ...
- 三种简单的html网页自动跳转方法
三种简单的html网页自动跳转方法,可以让你在打开一个html网页时自动跳转到其它的页面. 方法/步骤 <html> <head> <title>正在跳转< ...
- 海思hi35xx 开发学习(1):海思媒体处理平台架构
处理平台架构图: 主要分为: 视频输入(VI):VI 模块捕获视频图像,可对其做剪切.去噪等处理,并输出多路不同分辨率的图像数据. 视频处理(VPSS):VPSS 模块接收 VI 和解码模块发送过来的 ...
- [软件工程]项目选择与NABCD模型分析
项目 内容 这个作业属于哪个课程 2019春季计算机学院软件工程(罗杰) 这个作业的要求在哪里 团队项目选择 这课程的目标是 以实践形式熟悉软件开发流程,团队开发,合作学习 本次作业对课程的帮助是 确 ...
- C语言编译器和IDE的选择
什么是编译器: CPU只认识几百个二进制形式的指令,C语言对CPU而言简直就是天书.C语言是用固定的词汇与格式组织起来,简单直观,程序员容易识别和理解. 这时候就需要一个工具,将C语言代码转换成CPU ...