[抄题]:

设计实现一个带有下列属性的二叉查找树的迭代器:

  • 元素按照递增的顺序被访问(比如中序遍历)
  • next()hasNext()的询问操作要求均摊时间复杂度是O(1)

对于下列二叉查找树,使用迭代器进行中序遍历的结果为 [1, 6, 10, 11, 12]

  10
/ \
1 11
\ \
6 12

[思维问题]:

[一句话思路]:

有next就全部进入stack并设末尾为空,同时有没有和stack的结果相反。

弹出一个点的同时,next要设置成cur.right,重新入栈。因为next需要往下传。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 先定义一个AddNodeToStack方法,能入栈的先都入栈。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构,为什么不用别的数据结构]:

stack:

左边先进先出,再压右边。

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

整数或其他数据类型的Peeking Iterator,弹出peek

public class BSTIterator {

    Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode next = null;
void addNodeToStack(TreeNode root) {
while (root != null) {
stack.push(root);
root = root.left;
}
}
public BSTIterator(TreeNode root) {
next = root;
} /** @return whether we have a next smallest number */
public boolean hasNext() {
if (next != null) {
addNodeToStack(next);
next = null;
}
return !stack.isEmpty();
} /** @return the next smallest number */
public int next() {
if (! hasNext()) {
return 0;
}
TreeNode cur = stack.pop();
next = cur.right;
return cur.val;
}
}

二叉查找树迭代器 · Binary Search Tree Iterator的更多相关文章

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

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

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

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

  3. 【leetcode】Binary Search Tree Iterator

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

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

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

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

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

  6. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  7. ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java

    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] Binary Search Tree Iterator 二叉搜索树迭代器

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

随机推荐

  1. 1016 Phone Bills (25 分)

    1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following rul ...

  2. 关于json_encode()的使用注意

    json_encode($json_str,true)在一般情况下可以返回一个数组,但当$json_str的字符编码是GBK或其它时,返回的是一个 空数组,必须用iconv(‘gbk’,‘ut8//I ...

  3. git log --author详解,这个是个模糊匹配

    git log --author=authorname --author=<pattern>, commits whose author matches any of the given ...

  4. golang web框架 beego 学习 (四) 连接mysql

    1 DB参数配置在app.conf appname = gowebProject httpport = runmode = dev [db] host= localhost port= databas ...

  5. jshint在bat批处理中闪退,代码中无法调用的问题

    先说解决办法:加个call eg: call jshint --version Pause 具体原因有空再更

  6. CSS3基础

    内容: 1.圆角 border-radius 2.阴影 text-shadow.box-shadow 3.渐变 linear.radial 4.rgba rgb+alpha opacity 5.tra ...

  7. JavaScript中的数组和字符串

    知识内容: 1.JavaScript中的数组 2.JavaScript中的字符串 一.JavaScript中的数组 1.JavaScript中的数组是什么 数组指的是数据的有序列表,每种语言基本上都有 ...

  8. Xshell图形界面启动

    https://blog.csdn.net/qq_27843481/article/details/50539797 增加内存:https://jingyan.baidu.com/article/4d ...

  9. zabbixzabbix

    一,安装文档    https://www.zabbix.com/documentation/4.0/zh/manual/installation/requirements zabbix3.2.6安装 ...

  10. C# ADO.NET 封装的增删改查

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...