题目链接

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. Cookie存储大小、个数限制

    一.浏览器允许每个域名所包含的cookie数: Microsoft指出InternetExplorer8增加cookie限制为每个域名50个,但IE7似乎也允许每个域名50个cookie. Firef ...

  2. html实现钝角效果;html实现限制一行字数的显示,超出的部分用省略号(....)来代替

    前端实现div框边角的钝化虽然简单,但是有时候突然想不到,特此写下几句实现方法,以便记忆. 实现div框四个角都钝角的操作:设置 div : border-radius=10px; 实现div框一个角 ...

  3. Eclipse Infrastructure

    Everything is plug-ins running on or loaded by plug-ins loader called by a small kernal which is an ...

  4. pcp分布式监控工具

    已经集成在redhat6.x版本里 http://pcp.io

  5. MySQL的prompt不生效的问题

    安装完MySQL之后,使用了自定义的配置文件来启动MySQL,发现配置在[mysql]中的prompt并没有生效 [root@MySQL56_L1 ~]# /usr/local/mysql/bin/m ...

  6. 怎样学习webpack - 走心分享

    很多朋友可能都知道webpack,也见过webpack的代码,但是不明白里面一坨一坨的东西是什么意思,到底有什么用处!我们每个人学习一个新东西可能都会有这个过程,但是我个人觉得webpack可能是最混 ...

  7. 实现pdf word在线浏览和下载

    这篇实现的是在线展示pdf和word并且不能显示下载和打印按钮 一 下载功能: 因为html5给a标签新添加了一个属性download,这个属性可以直接实现下载文件的功能:<a href=&qu ...

  8. Codeforces - Educational Codeforces Round 5 - E. Sum of Remainder

    题目链接:http://codeforces.com/contest/616/problem/E 题目大意:给定整数n,m(1≤n,m≤1013), 求(n mod 1 + n mod 2 + ... ...

  9. 将TIMESTAMP类型的差值转化为秒的方法

    两个TIMESTAMP之差得到的是INTERVAL类型,而有时我们只需要得到两个时间相差的秒数,如果变成INTERVAL之后,想要获取这个值会非常麻烦. 比较常见的方法是使用EXTRACT来抽取获得的 ...

  10. CentOS下用rinetd做端口转发

    windows下的端口转发一般用的是自带的nat和porttunnel.portmap linux下端口转发映射的程序叫rinetd,启动方法rinetd -c /etc/rinetd.conf  , ...