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相关推荐

  1. 173. Binary Search Tree Iterator

    一.题目 1.审题 2.分析 给出一棵二分查找树的根节点.实现 next() 方法返回下一个最小的二叉树的节点值. hasNext() 判断是否还有值. 二.解答 1.思路: 采用一个 Stack 存 ...

  2. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

  3. [leetcode] Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  4. LeetCode:Validate Binary Search Tree

    题目链接 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  5. LeetCode Binary Search Tree Iterator( 二叉树迭代器)

    问题:给出一棵树,使用迭代器来输出. 思路:以中序遍历方式来输出.先中序遍历存入数组中,然后遍历.另一种方式是以模拟栈形式来中序遍历,先将左子树入栈,在遍历过程中,如果当前结点有右子树,需要将右子树按 ...

  6. LeetCode Validate Binary Search Tree(dfs)

    问题:判断树是否是合法的二叉搜索树 思路: 1.在dfs时,判断当前结点是否大于左子树的最大值,小于右子树的最小值.这是一种思路 2.另外一个思路是在中序遍历时,用个变量记录当前结点的前驱结点,再判断 ...

  7. Leetcode: Validate Binary Search Tree

    思路: 1. 难点在于构造递归函数的参数 2. 参数要包含上下界, 才能具有全局性. 第一次提交 WA 了, 因为写成来的判断函数是局部性的 代码: const int POS = 1E9; cons ...

  8. 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 ...

  9. 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 ...

最新文章

  1. linux调试C++错误: 程序中有游离的‘\240’‘\302’
  2. 使用javaMail收邮件 支持附件下载
  3. DotNetMagic 5.4.0破解
  4. WPF 用代码实现WrapPanel右侧自动对齐(解决多余空白问题)
  5. hibernate session 新增、删除,修改、查询样例
  6. Navicat Premium 实用快捷键
  7. 每日一题(30)—— 局部变量能否和全局变量重名?
  8. c语言 统计数量用count_请问c语言当中,如何统计数字各自出现的次数
  9. 信息学奥赛一本通C++语言——1077:统计满足条件的4位数
  10. CodeForces 589J Cleaner Robot
  11. 微信小程序申请 软件著作权(其它 软件,小程序都可以)
  12. GreenSock (TweenMax) 动画案例(一)
  13. 新版Google工具栏(For Firefox)发布
  14. 力扣报错 error: <identifier> expected
  15. HTML识别文本空格回车换行展示
  16. linux查看tomcat在,在陌生Linux环境查看Tomcat服务的方法
  17. Linux下安装libgdal库,libjpeg库和libtiff库
  18. 按图搜索1688商品(拍立淘) 中国站
  19. catia 草绘轮廓
  20. Pre-Switch宣布推出采用人工智能的跨平台软开关技术

热门文章

  1. 【整理】22款免费在线图表制作工具
  2. Python:计算及格率和优秀率,用百分数表示,百分号前的部分四舍五入保留整数
  3. Java Number 类和方法
  4. 简单了解一下LCD屏工作原理
  5. 用vue写轮子的一些心得(五)——Slides轮播组件
  6. MATLAB函数解析:colormap——查看并设置当前颜色图
  7. iphone6安装android,在iPhone里装了真 Android系统 这事情已经很神奇了
  8. 人行征信2.0对接服务:全业务种类数据,精细您的征信业务管理!
  9. Python 之 Anaconda
  10. HarmonyOS上玩“语音识别”