时间挤挤总是有的

太久不做题,脑子都生锈了。来道水题练练手

题目地址:

https://leetcode.com/problems/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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

题目解析:

BST中序遍历,难点(算么 =。=)在于不可以简单一次递归完成,而是需要停止继续机制。

注意到要求时间均摊复杂度O(1),和不超过树高的空间复杂度。

不妨定义一个栈如下:

LinkedList<TreeNode> stack = new LinkedList<TreeNode>();

在此维护一个性质:栈顶的元素是当前的最小元素。

因此,初始化栈的时候,需要把root的所有左节点压入栈中。每当请求next的时候,就返回栈顶节点的值,同时,如果栈顶节点有右节点,就需要把右节点和他所有的左子节点都压入栈中。

具体代码:

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator { LinkedList<TreeNode> stack = new LinkedList<TreeNode>(); // 在处理栈顶这个节点以前,他的左子树已经被处理过了,换言之,栈顶必然是当前最小节点 public BSTIterator(TreeNode root) { while (root != null) {
stack.push(root);
root = root.left;
} } /**
* @return whether we have a next smallest number
*/
public boolean hasNext() {
return stack.size() != 0;
} /**
* @return the next smallest number
*/
public int next() {
TreeNode node = stack.pop();
if (node.right != null) {
TreeNode left = node.right;
while (left != null) {
stack.push(left);
left = left.left;
}
}
return node.val;
}
} /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/

复杂度分析:

可以看出,任意时刻,栈中的元素数量不会超过树高。这个画一个图就明白了。

由于每个节点会被压入栈中 && 被访问值一次,所以对每个节点的操作数都是2,虽然访问某些特定节点时,需要更多的压栈操作,但由于next方法可能只执行一次访问值操作,所以实际上更多的压栈操作是分摊到了不需要压栈的时刻中了。

所以均摊是O(1)。

【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告的更多相关文章

  1. LeetCode: Binary Search Tree Iterator 解题报告

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  2. 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...

  3. 【LeetCode】270. Closest Binary Search Tree Value 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  4. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  5. 【leetcode】Binary Search Tree Iterator

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  6. leetcode-173:Binary Search Tree Iterator(Java)

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  7. 【LeetCode】173. Binary Search Tree Iterator (2 solutions)

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  8. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  9. Java for LeetCode 173 Binary Search Tree Iterator

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

随机推荐

  1. Tokyo Tyrant(TTServer)系列(四)-tcrmgr远程管理与调试

    Tokyo Tyrant(TTServer)系列-tcrmgr(远程管理与调试) tcrmgr是TokyoTyrant的管理工具,对ttserver进行管理与执行命令: 通过输入tcrmgr回车,能够 ...

  2. 带有机器人框架的.NET自己主动化測试

    Clayton Neal在软件測试和质量保证方面有超过13年的经验,当中有八年的Windows, web,和移动应用程序的測试自己主动化经验.他在測试领域的全部等级都工作过.近期他在Bloomberg ...

  3. Swift - 基本数据类型,及常/变量声明

    2015-01-08 14:59 发布:yuhang 浏览:434 下面是Swift中基本的数据类型介绍说明: 1,变量:使用var声明 1 var str:String = "hangge ...

  4. PAIP: Paradigms of Artificial Intelligence Programming

    PAIP: Paradigms of Artificial Intelligence Programming PAIP: Paradigms of Artificial Intelligence Pr ...

  5. http staus汇总

    参考http://www.cnblogs.com/cxd4321/archive/2008/11/20/1337776.html 常见HTTP状态码 200 OK 301 Moved Permanen ...

  6. How-To: add EPEL repository to Centos 6.x is Easy!

    How-To: add EPEL repository to Centos 6.x is Easy! | ITek Blog How-To: add EPEL repository to Centos ...

  7. ZOJ 1171 Sorting the Photos

    1. 题目描述 给你一叠照片,有的正面朝上,有的反面朝上,朝上的用字母U,朝下的用字母D 可以从一个位置开始到最顶端,把这一叠拿出来,反转,然后再放回那一叠照片上面. 试求出最少的翻转次数,使所有的照 ...

  8. Linux 利用hosts.deny 防止暴力破解ssh(转)

    一.ssh暴力破解 利用专业的破解程序,配合密码字典.登陆用户名,尝试登陆服务器,来进行破解密码,此方法,虽慢,但却很有效果. 二.暴力破解演示 2.1.基础环境:2台linux主机(centos 7 ...

  9. SaaS怎样改变了商务世界

    当下,全球的经济环境愈发复杂,竞争日益激烈,这就要求企业负责人高速适应和调整战略应对挑战.假设你的企业可以优化内部操作流程,走在新技术的前沿,你就行减少成本.改善服务质量.没有及时应对的企业非常快就会 ...

  10. DOS批处理延时技术

    DOS批处理延时技术 举个例子,我们要延时5秒打开gdh.txt这个文件,可以用以下几个方法 方法一:ping  缺点:时间精度为1秒,不够精确   www.2cto.com   @echo off  ...