Skip to content

Commit c31b8eb

Browse files
Merge pull request SharingSource#519 from SharingSource/ac_oier
✨feat: Add 面试题 04.06
2 parents 4d4893d + ca8122e commit c31b8eb

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

Index/DFS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@
3939
| [1723. 完成所有工作的最短时间](https://leetcode-cn.com/problems/find-minimum-time-to-finish-all-jobs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-minimum-time-to-finish-all-jobs/solution/gong-shui-san-xie-yi-ti-shuang-jie-jian-4epdd/) | 困难 | 🤩🤩🤩 |
4040
| [1766. 互质树](https://leetcode-cn.com/problems/tree-of-coprimes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/tree-of-coprimes/solution/bu-tai-yi-yang-de-dfs-ji-lu-suo-you-zui-d3xeu/) | 困难 | 🤩🤩🤩🤩 |
4141
| [2044. 统计按位或能得到最大值的子集数目](https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets/solution/by-ac_oier-dos6/) | 困难 | 🤩🤩🤩🤩 |
42+
| [面试题 04.06. 后继者](https://leetcode.cn/problems/successor-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/successor-lcci/solution/by-ac_oier-xib5/) | 中等 | 🤩🤩🤩🤩🤩 |
4243

Index/二叉树.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
| [1104. 二叉树寻路](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-rw2d/) | 中等 | 🤩🤩🤩 |
1717
| [1305. 两棵二叉搜索树中的所有元素](https://leetcode-cn.com/problems/all-elements-in-two-binary-search-trees/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/all-elements-in-two-binary-search-trees/solution/by-ac_oier-c8fv/) | 中等 | 🤩🤩🤩🤩 |
1818
| [剑指 Offer 37. 序列化二叉树](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/solution/gong-shui-san-xie-er-cha-shu-de-xu-lie-h-n89a/) | 困难 | 🤩🤩🤩🤩🤩 |
19+
| [面试题 04.06. 后继者](https://leetcode.cn/problems/successor-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/successor-lcci/solution/by-ac_oier-xib5/) | 中等 | 🤩🤩🤩🤩🤩 |
1920

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[面试题 04.06. 后继者](https://leetcode.cn/problems/successor-lcci/solution/by-ac_oier-xib5/)** ,难度为 **中等**
4+
5+
Tag : 「BST」、「递归」
6+
7+
8+
9+
设计一个算法,找出二叉搜索树中指定节点的 “下一个” 节点(也即中序后继)。
10+
11+
如果指定节点没有对应的 “下一个” 节点,则返回 `null`
12+
13+
示例 1:
14+
```
15+
输入: root = [2,1,3], p = 1
16+
17+
2
18+
/ \
19+
1 3
20+
21+
输出: 2
22+
```
23+
示例 2:
24+
```
25+
输入: root = [5,3,6,2,4,null,null,1], p = 6
26+
27+
5
28+
/ \
29+
3 6
30+
/ \
31+
2 4
32+
/
33+
1
34+
35+
输出: null
36+
```
37+
38+
---
39+
40+
### BST 特性 + 递归
41+
42+
利用 `BST` 的特性,我们可以根据当前节点 `root``p` 的值大小关系来确定搜索方向:
43+
44+
1. 若有 `root.val <= p.val` : 根据 `BST` 特性可知当前节点 `root` 及其左子树子节点均满足「值小于等于 `p.val`」,因此不可能是 `p` 点的后继,我们直接到 `root` 的右子树搜索 `p` 的后继(递归处理);
45+
2. 若有 `root.val > p.val` : 当第一次搜索到满足此条件的节点时,在以 `root` 为根节点的子树中「位于最左下方」的值为 `p` 的后继,但也有可能 `root` 没有左子树,因此 `p` 的后继要么在 `root` 的左子树中(若有),要么是 `root` 本身,此时我们可以直接到 `root` 的左子树搜索,若搜索结果为空返回 `root`,否则返回搜索结果。
46+
47+
代码:
48+
```Java
49+
class Solution {
50+
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
51+
if (root == null) return null;
52+
if (root.val <= p.val) return inorderSuccessor(root.right, p);
53+
TreeNode ans = inorderSuccessor(root.left, p);
54+
return ans == null ? root : ans;
55+
}
56+
}
57+
```
58+
* 时间复杂度:复杂度与深度相关,最坏情况下仍会搜索所有节点,复杂度为 $O(n)$
59+
* 空间复杂度:忽略递归带来的额外空间消耗,复杂度为 $O(1)$
60+
61+
---
62+
63+
### 最后
64+
65+
这是我们「刷穿 LeetCode」系列文章的第 `No.面试题 04.06` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
66+
67+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
68+
69+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
70+
71+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
72+

0 commit comments

Comments
 (0)