题目链接

https://leetcode.com/problems/binary-search-tree-iterator/description/

题目描述

实现一个二叉搜索树迭代器。你将使用二叉搜索树的根节点初始化迭代器。

调用 next() 将返回二叉搜索树中的下一个最小的数。

注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 O(h) 内存,其中 h 是树的高度。

题解

代码

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator { private Stack<TreeNode> stack = new Stack<>();
public BSTIterator(TreeNode root) {
pushAll(root);
} /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.isEmpty();
} /** @return the next smallest number */
public int next() {
TreeNode tmpNode = stack.pop();
pushAll(tmpNode.right);
return tmpNode.val;
} private void pushAll(TreeNode node) {
for (; node != null; stack.push(node), node = node.left);
}
} /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/

Leetcode 173. 二叉搜索树迭代器的更多相关文章

  1. Java实现 LeetCode 173 二叉搜索树迭代器

    173. 二叉搜索树迭代器 实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 示例: BSTIterator iterato ...

  2. 173 Binary Search Tree Iterator 二叉搜索树迭代器

    实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...

  3. [Swift]LeetCode173. 二叉搜索树迭代器 | Binary Search Tree Iterator

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  4. 刷题-力扣-剑指 Offer II 055. 二叉搜索树迭代器

    剑指 Offer II 055. 二叉搜索树迭代器 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kTOapQ 著作权归领扣网络所有 ...

  5. leetcode_173【二叉搜索树迭代器】

    实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 示例: BSTIterator iterator = new BSTIte ...

  6. Leetcode173. Binary Search Tree Iterator二叉搜索树迭代器

    实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 注意: next() 和hasNext() 操作的时间复杂度是O(1),并 ...

  7. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  8. LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. LeetCode 把二叉搜索树转换为累加树

    第538题 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和. 例如: 输入: 二叉 ...

随机推荐

  1. Dreams save us. Dreams lift us up and transform us into something better.

    Dreams save us. Dreams lift us up and transform us into something better.梦想能够拯救我们.梦想能够激励我们并让我们成为更好的人 ...

  2. css取消a标签在移动端点击时的背景颜色

    一.取消a标签在移动端点击时的蓝色 -webkit-tap-highlight-color: rgba(255, 255, 255, 0);-webkit-user-select: none;-moz ...

  3. 【起航计划 026】2015 起航计划 Android APIDemo的魔鬼步伐 25 App->Notification->Status Bar 状态栏显示自定义的通知布局,省却声音、震动

    这个例子的Icons Only 和 Icons and marquee 没有什么特别好说明的. 而Use Remote views in balloon 介绍了可以自定义在Extended Statu ...

  4. XCode 如何真机运行别人的demo项目

    iOS应用安装到真机需要证书和mobileprovision 文件,拿到别人的项目 是没有这些的 ,也就运行不起来. 要想运行起来, 需要选中项目,  target - > 修改 bundlei ...

  5. linux服务器tomcat启动时,不能加载项目

    ---恢复内容开始--- 问题描述:tomcat启动时候,能够成功启动但是项目不能加载,查看catalina.out后出现以下的错误 due to a StackOverflowError. Poss ...

  6. IIS7 配置SSL 绑定主机头

    IIS7下面默认HTTPS绑定是无法指定主机头的,我们可以通过手工修改IIS配置来实现主机头绑定. 首先停止IIS服务. 然后打开C:/Windows/system32/inetsrv/config/ ...

  7. Prestashop-1.6.1.6-zh_CN (Openlogic CentOS 7.2)

    平台: CentOS 类型: 虚拟机镜像 软件包: prestashop1.6.1.6 commercial content management ecommerce open-source 简体中文 ...

  8. 【转载】#336 - Declaring and Using a readonly Field

    You can make a field in a class read-only by using the readonly modifier when the field is declared. ...

  9. "提取位于北坡的各类用地面积信息"的程序设计与实现

    "提取位于北坡的各类用地面积信息"的程序设计与实现  程序员:左正康         发表时间:2013/12/20 14:24       代号:黑眼圈的日子 第一步:导入dem ...

  10. selenium+chrome下载文件,格式怎么选择???

    学习了下载 if browser == "Chrome": options=webdriver.ChromeOptions() prefs={'profile.default_co ...