Skip to content

Commit c96571a

Browse files
Merge pull request SharingSource#656 from SharingSource/ac_oier
✨feat : add 998
2 parents a495093 + 2eceea8 commit c96571a

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
| [953. 验证外星语词典](https://leetcode.cn/problems/verifying-an-alien-dictionary/) | [LeetCode 题解链接](https://leetcode.cn/problems/verifying-an-alien-dictionary/solution/by-ac_oier-sxf1/) | 简单 | 🤩🤩🤩🤩 |
123123
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |
124124
| [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 |
125+
| [998. 最大二叉树 II](https://leetcode.cn/problems/maximum-binary-tree-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-binary-tree-ii/solution/by-ac_oier-v82s/) | 中等 | 🤩🤩🤩🤩 |
125126
| [1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/) | 困难 | 🤩🤩🤩🤩 |
126127
| [1005. K 次取反后最大化的数组和](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/solution/gong-shui-san-xie-jian-dan-fen-qing-kuan-6qwu/) | 简单 | 🤩🤩🤩🤩 |
127128
| [1021. 删除最外层的括号](https://leetcode.cn/problems/remove-outermost-parentheses/) | [LeetCode 题解链接](https://leetcode.cn/problems/remove-outermost-parentheses/solution/by-ac_oier-jmxi/) | 简单 | 🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[998. 最大二叉树 II](https://leetcode.cn/problems/maximum-binary-tree-ii/solution/by-ac_oier-v82s/)** ,难度为 **中等**
4+
5+
Tag : 「迭代」、「模拟」
6+
7+
8+
9+
最大树 定义:一棵树,并满足:其中每个节点的值都大于其子树中的任何其他值。
10+
11+
给你最大树的根节点 `root` 和一个整数 `val`
12+
13+
就像 之前的问题 那样,给定的树是利用 `Construct(a)` 例程从列表 `a``root = Construct(a)`)递归地构建的:
14+
15+
* 如果 `a` 为空,返回 `null`
16+
* 否则,令 `a[i]` 作为 `a` 的最大元素。创建一个值为 `a[i]` 的根节点 `root`
17+
* `root` 的左子树将被构建为 `Construct([a[0], a[1], ..., a[i - 1]])`
18+
* `root` 的右子树将被构建为 `Construct([a[i + 1], a[i + 2], ..., a[a.length - 1]])`
19+
* 返回 `root`
20+
21+
请注意,题目没有直接给出 `a` ,只是给出一个根节点 `root = Construct(a)`
22+
23+
假设 `b``a` 的副本,并在末尾附加值 `val`。题目数据保证 `b` 中的值互不相同。
24+
25+
返回 `Construct(b)`
26+
27+
示例 1:
28+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/23/maximum-binary-tree-1-2.png)
29+
```
30+
输入:root = [4,1,3,null,null,2], val = 5
31+
32+
输出:[5,4,null,1,3,null,null,2]
33+
34+
解释:a = [1,4,2,3], b = [1,4,2,3,5]
35+
```
36+
示例 2:
37+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/23/maximum-binary-tree-2-2.png)
38+
```
39+
输入:root = [5,2,4,null,1], val = 3
40+
41+
输出:[5,2,4,null,1,null,3]
42+
43+
解释:a = [2,1,5,4], b = [2,1,5,4,3]
44+
```
45+
示例 3:
46+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/23/maximum-binary-tree-3-2.png)
47+
```
48+
输入:root = [5,2,3,null,1], val = 4
49+
50+
输出:[5,2,4,null,1,3]
51+
52+
解释:a = [2,1,5,3], b = [2,1,5,3,4]
53+
```
54+
55+
提示:
56+
* 树中节点数目在范围 $[1, 100]$ 内
57+
* $1 <= Node.val <= 100$
58+
* 树中的所有值 互不相同
59+
* $1 <= val <= 100$
60+
61+
---
62+
63+
### 模拟
64+
65+
题意不是很好理解,先稍微解释一下吧。
66+
67+
大概意思是最大树 `root` 是根据特定的规则构造出来的,即给定的 `root` 其实对应一个具体的 `nums`,题目要求是将 `val` 追加到 `nums` 的尾部,然后再对得到的 `nums` 运用相同规则的构造,返回重新构造的最大树头结点。
68+
69+
根据构造规则,若有下标 $i < j$,则 $nums[i]$ 必然在 $nums[j]$ 水平线的左边,而 `val` 又是追加在原有 `nums` 的结尾。因此其最终位置分如下两种情况:
70+
71+
* `val` 为新 `nums` 中的最大值,同时 `val` 又是追加在原有 `nums` 的结尾,此时将原有的 `root` 挂在 `val` 对应节点的左子树即可,新树的根节点为 `val` 对应节点;
72+
* 否则,我们只需要不断在 `root` 的右子树中找目标位置(反证法可以知,`val` 必不可能出现在任一非右位置,否则可推断出在 `val` 右边仍有元素,这与 `val` 位于 `nums` 的结尾位置冲突)。假设目标位置的父节点为 `prev`,目标位置的原节点为 `cur`,根据构造规则可知 `prev.right = node``node.left = cur`,新树的根节点不变。
73+
74+
Java 代码:
75+
```Java
76+
class Solution {
77+
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
78+
TreeNode node = new TreeNode(val);
79+
TreeNode prev = null, cur = root;
80+
while (cur != null && cur.val > val) {
81+
prev = cur; cur = cur.right;
82+
}
83+
if (prev == null) {
84+
node.left = cur;
85+
return node;
86+
} else {
87+
prev.right = node;
88+
node.left = cur;
89+
return root;
90+
}
91+
}
92+
}
93+
```
94+
Typescript 代码:
95+
```Typescript
96+
function insertIntoMaxTree(root: TreeNode | null, val: number): TreeNode | null {
97+
const node = new TreeNode(val)
98+
let prev = null, cur = root
99+
while (cur != null && cur.val > val) {
100+
prev = cur; cur = cur.right
101+
}
102+
if (prev == null) {
103+
node.left = root
104+
return node
105+
} else {
106+
prev.right = node
107+
node.left = cur
108+
return root
109+
}
110+
};
111+
```
112+
* 时间复杂度:$O(n)$
113+
* 空间复杂度:$O(1)$
114+
115+
---
116+
117+
### 最后
118+
119+
这是我们「刷穿 LeetCode」系列文章的第 `No.998` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
120+
121+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
122+
123+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
124+
125+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
126+

0 commit comments

Comments
 (0)