LC 173. 二叉搜索树迭代器

题目描述

这是 LeetCode 上的 173. 二叉搜索树迭代器 ,难度为 中等

实现一个二叉搜索树迭代器类 BSTIterator,表示一个按中序遍历二叉搜索树(BST)的迭代器:

  • BSTIterator(TreeNode root) 初始化 BSTIterator 类的一个对象。BST 的根节点 root 会作为构造函数的一部分给出。指针应初始化为一个不存在于 BST 中的数字,且该数字小于 BST 中的任何元素。
  • boolean hasNext() 如果向指针右侧遍历存在数字,则返回 true;否则返回 false
  • int next() 将指针向右移动,然后返回指针处的数字。

注意,指针初始化为一个不存在于 BST 中的数字,所以对 next() 的首次调用将返回 BST 中的最小元素。

你可以假设 next() 调用总是有效的,也就是说,当调用 next() 时,BST 的中序遍历中至少存在一个下一个数字。

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
输入
["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
输出
[null, 3, 7, true, 9, true, 15, true, 20, false]

解释
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
bSTIterator.next(); // 返回 3
bSTIterator.next(); // 返回 7
bSTIterator.hasNext(); // 返回 True
bSTIterator.next(); // 返回 9
bSTIterator.hasNext(); // 返回 True
bSTIterator.next(); // 返回 15
bSTIterator.hasNext(); // 返回 True
bSTIterator.next(); // 返回 20
bSTIterator.hasNext(); // 返回 False

提示:

  • 树中节点的数目在范围 [1, $10^5$] 内
  • $0 <= Node.val <= 10^6$
  • 最多调用 $10^5$ 次 hasNextnext 操作

进阶:

  • 你可以设计一个满足下述条件的解决方案吗?next()hasNext() 操作均摊时间复杂度为 $O(1)$ ,并使用 $O(h)$ 内存。其中 h 是树的高度。

基本思路

这道题本质上考的是「将迭代版的中序遍历代码」做等价拆分。

我们知道,中序遍历的基本逻辑是:处理左子树 -> 处理当前节点 -> 处理右子树。

其中迭代做法是利用「栈」进行处理:

  1. 先将当前节点的所有左子树压入栈,压到没有为止
  2. 将最后一个压入的节点弹出(栈顶元素),加入答案
  3. 将当前弹出的节点作为当前节点,重复步骤一

相应的裸题在这里:94. 二叉树的中序遍历

中序遍历的迭代代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
List<Integer> ans = new ArrayList<>();
Deque<TreeNode> d = new ArrayDeque<>();
public List<Integer> inorderTraversal(TreeNode root) {
while (root != null || !d.isEmpty()) {
// 步骤 1
while (root != null) {
d.addLast(root);
root = root.left;
}

// 步骤 2
root = d.pollLast();
ans.add(root.val);

// 步骤 3
root = root.right;
}
return ans;
}
}

总的来说是这么一个迭代过程:步骤 1 -> 步骤 2 -> 步骤 3 -> 步骤 1 …


「中序遍历」代码的「等价拆分」

首先因为 next() 方法中我们需要输出一个值,执行的的是「步骤 2」的逻辑,同时我们需要在其前后添加「步骤 1」和「步骤 3」。

另外,我们还有一个 hasNext() 要处理,显然 hasNext() 应该对应我们的栈是否为空。

为此,我们需要确保每次输出之后「步骤 1」被及时执行。

综上,我们应该在初始化时,走一遍「步骤 1」,然后在 next() 方法中走「步骤 2」、「步骤 3」和「步骤 1」。

代码:

[]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class BSTIterator {
Deque<TreeNode> d = new ArrayDeque<>();
public BSTIterator(TreeNode root) {
// 步骤 1
dfsLeft(root);
}

public int next() {
// 步骤 2
TreeNode root = d.pollLast();
int ans = root.val;
// 步骤 3
root = root.right;
// 步骤 1
dfsLeft(root);
return ans;
}

void dfsLeft(TreeNode root) {
while (root != null) {
d.addLast(root);
root = root.left;
}
}

public boolean hasNext() {
return !d.isEmpty();
}
}

  • 时间复杂度:由于每个元素都是严格「进栈」和「出栈」一次,复杂度为均摊 $O(1)$
  • 空间复杂度:栈内最多保存与深度一致的节点数量,复杂度为 $O(h)$

进阶

事实上,我们空间复杂度也能做到 $O(1)$,该如何做呢?


最后

这是我们「刷穿 LeetCode」系列文章的第 No.173 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!