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 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.
解题思路
构建中序遍历的线索二叉树(利用哈希表(unordered_map
)来存储线索), 线索的顺序就是迭代器遍历的顺序。
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) {TreeNode* pre = nullptr;makeTreeNextMap(root, pre);tree2Next[pre] = nullptr;curNode = tree2Next[nullptr];}/** @return whether we have a next smallest number */bool hasNext() {return !(curNode == nullptr);}/** @return the next smallest number */int next() {int cur = curNode->val;curNode = tree2Next[curNode];return cur;}
private:void makeTreeNextMap(TreeNode* root, TreeNode*& pre){if(root == nullptr) return ;makeTreeNextMap(root->left, pre);tree2Next[pre] = root;pre=root;makeTreeNextMap(root->right, pre);}private:unordered_map<TreeNode*, TreeNode*> tree2Next; TreeNode* curNode;
};/*** Your BSTIterator will be called like this:* BSTIterator i = BSTIterator(root);* while (i.hasNext()) cout << i.next();*/
LeetCode: 173. Binary Search Tree Iterator相关推荐
- 173. Binary Search Tree Iterator
一.题目 1.审题 2.分析 给出一棵二分查找树的根节点.实现 next() 方法返回下一个最小的二叉树的节点值. hasNext() 判断是否还有值. 二.解答 1.思路: 采用一个 Stack 存 ...
- LeetCode Closest Binary Search Tree Value II
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...
- [leetcode] Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- LeetCode:Validate Binary Search Tree
题目链接 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- LeetCode Binary Search Tree Iterator( 二叉树迭代器)
问题:给出一棵树,使用迭代器来输出. 思路:以中序遍历方式来输出.先中序遍历存入数组中,然后遍历.另一种方式是以模拟栈形式来中序遍历,先将左子树入栈,在遍历过程中,如果当前结点有右子树,需要将右子树按 ...
- LeetCode Validate Binary Search Tree(dfs)
问题:判断树是否是合法的二叉搜索树 思路: 1.在dfs时,判断当前结点是否大于左子树的最大值,小于右子树的最小值.这是一种思路 2.另外一个思路是在中序遍历时,用个变量记录当前结点的前驱结点,再判断 ...
- Leetcode: Validate Binary Search Tree
思路: 1. 难点在于构造递归函数的参数 2. 参数要包含上下界, 才能具有全局性. 第一次提交 WA 了, 因为写成来的判断函数是局部性的 代码: const int POS = 1E9; cons ...
- LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List--转换二叉树为双向链表--Java,C++,Python解法
题目地址:Convert Binary Search Tree to Sorted Doubly Linked List - LeetCode Convert a BST to a sorted ci ...
- LeetCode: 109. Convert Sorted List to Binary Search Tree
题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height b ...
最新文章
- linux调试C++错误: 程序中有游离的‘\240’‘\302’
- 使用javaMail收邮件 支持附件下载
- DotNetMagic 5.4.0破解
- WPF 用代码实现WrapPanel右侧自动对齐(解决多余空白问题)
- hibernate session 新增、删除,修改、查询样例
- Navicat Premium 实用快捷键
- 每日一题(30)—— 局部变量能否和全局变量重名?
- c语言 统计数量用count_请问c语言当中,如何统计数字各自出现的次数
- 信息学奥赛一本通C++语言——1077:统计满足条件的4位数
- CodeForces 589J Cleaner Robot
- 微信小程序申请 软件著作权(其它 软件,小程序都可以)
- GreenSock (TweenMax) 动画案例(一)
- 新版Google工具栏(For Firefox)发布
- 力扣报错 error: <identifier> expected
- HTML识别文本空格回车换行展示
- linux查看tomcat在,在陌生Linux环境查看Tomcat服务的方法
- Linux下安装libgdal库,libjpeg库和libtiff库
- 按图搜索1688商品(拍立淘) 中国站
- catia 草绘轮廓
- Pre-Switch宣布推出采用人工智能的跨平台软开关技术
热门文章
- 【整理】22款免费在线图表制作工具
- Python:计算及格率和优秀率,用百分数表示,百分号前的部分四舍五入保留整数
- Java Number 类和方法
- 简单了解一下LCD屏工作原理
- 用vue写轮子的一些心得(五)——Slides轮播组件
- MATLAB函数解析:colormap——查看并设置当前颜色图
- iphone6安装android,在iPhone里装了真 Android系统 这事情已经很神奇了
- 人行征信2.0对接服务:全业务种类数据,精细您的征信业务管理!
- Python 之 Anaconda
- HarmonyOS上玩“语音识别”