[抄题]:

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

  • 元素按照递增的顺序被访问(比如中序遍历)
  • 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. ossfs工具将OSS挂载到阿里云linux系统目录例子

    ossfs 是基于 aliyun OSS 的 fuse 客户端了,所以我们把它用在阿里云上肯定是没有错了,下面来看一篇关于ossfs工具将OSS挂载到阿里云linux系统目录例子,具体的细节如下文介绍 ...

  2. /PROC/MEMINFO之谜

    网站转自:http://linuxperf.com/?p=142 非常技术的网站,够看上一阵子的(一篇文章) /proc/meminfo是了解Linux系统内存使用状况的主要接口,我们最常用的”fre ...

  3. JavaScript的灵活应用

    1.查找数组的最大值和最小值 (1) Math.max.qpply(null,array); Math.min.qpply(null,array); (2) eval("Math.max(& ...

  4. Python - Django - 登录页面

    登录页 login.html: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta char ...

  5. 通过python给mysql建表

    一.python连接mysql from sqlalchemy import create_engine # 数据库数据 HOSTNAME = '127.0.0.1' # linux本地 PORT = ...

  6. python读取excel,数字都是浮点型,日期格式是数字的解决办法

    excel文件内容: 读取excel: # coding=utf-8 import xlrd import sys reload(sys) sys.setdefaultencoding('utf-8' ...

  7. rabbitMQ 的基本知识

    参考:  https://www.cnblogs.com/dwlsxj/p/RabbitMQ.html

  8. mysql 5.7新特新 操作json 数组

    ; UPDATE EDI.edi_history SET response_summary = JSON_REPLACE(response_summary, ; 对于json数组,使用$[*]  然后 ...

  9. leetcode35

    public class Solution { public int SearchInsert(int[] nums, int target) { ; i < nums.Length; i++) ...

  10. DevExpress ImageComboBoxEdit增加

    Combo_订单类型.Properties.Items.Clear() Select Case Combo_客户名称.EditValue Case "ABC" Combo_订单类型 ...