Skip to content

Commit 7b13eaf

Browse files
committed
✨feat: Add 559
1 parent cb14a06 commit 7b13eaf

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

Index/BFS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| [297. 二叉树的序列化与反序列化](https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/) | [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/) | 困难 | 🤩🤩🤩🤩 |
55
| [397. 整数替换](https://leetcode-cn.com/problems/integer-replacement/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/integer-replacement/solution/gong-shui-san-xie-yi-ti-san-jie-dfsbfs-t-373h/) | 中等 | 🤩🤩🤩🤩 |
66
| [403. 青蛙过河](https://leetcode-cn.com/problems/frog-jump/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/frog-jump/solution/gong-shui-san-xie-yi-ti-duo-jie-jiang-di-74fw/) | 困难 | 🤩🤩🤩🤩 |
7+
| [559. N 叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-b-n956/) | 简单 | 🤩🤩🤩🤩 |
78
| [690. 员工的重要性](https://leetcode-cn.com/problems/employee-importance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/employee-importance/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-s79x/) | 简单 | 🤩🤩🤩 |
89
| [778. 水位上升的泳池中游泳](https://leetcode-cn.com/problems/swim-in-rising-water/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/swim-in-rising-water/solution/gong-shui-san-xie-yi-ti-shuang-jie-krusk-7c6o/) | 困难 | 🤩🤩🤩 |
910
| [783. 二叉搜索树节点最小距离](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/solution/gong-shui-san-xie-yi-ti-san-jie-shu-de-s-7r17/) | 简单 | 🤩🤩🤩 |

Index/DFS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
| [437. 路径总和 III](https://leetcode-cn.com/problems/path-sum-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-sum-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-q-usa7/) | 中等 | 🤩🤩🤩🤩 |
1515
| [488. 祖玛游戏](https://leetcode-cn.com/problems/zuma-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/zuma-game/solution/gong-shui-san-xie-yi-ti-shuang-jie-sou-s-3ftb/) | 困难 | 🤩🤩🤩🤩 |
1616
| [494. 目标和](https://leetcode-cn.com/problems/target-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/target-sum/solution/gong-shui-san-xie-yi-ti-si-jie-dfs-ji-yi-et5b/) | 中等 | 🤩🤩🤩🤩 |
17+
| [559. N 叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-b-n956/) | 简单 | 🤩🤩🤩🤩 |
1718
| [563. 二叉树的坡度](https://leetcode-cn.com/problems/binary-tree-tilt/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-tree-tilt/solution/gong-shui-san-xie-jian-dan-er-cha-shu-di-ekz4/) | 简单 | 🤩🤩🤩🤩 |
1819
| [638. 大礼包](https://leetcode-cn.com/problems/shopping-offers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shopping-offers/solution/gong-shui-san-xie-yi-ti-shuang-jie-zhuan-qgk1/) | 中等 | 🤩🤩🤩🤩 |
1920
| [677. 键值映射](https://leetcode-cn.com/problems/map-sum-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/map-sum-pairs/solution/gong-shui-san-xie-jie-he-dfs-de-trie-yun-i4xa/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[559. N 叉树的最大深度](https://leetcode-cn.com/problems/brick-wall/solution/gong-shui-san-xie-zheng-nan-ze-fan-shi-y-gsri/)** ,难度为 **简单**
4+
5+
Tag : 「DFS」、「BFS」
6+
7+
8+
9+
给定一个 `N` 叉树,找到其最大深度。
10+
11+
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
12+
13+
`N` 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。
14+
15+
示例 1:
16+
![](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)
17+
```
18+
输入:root = [1,null,3,2,4,null,5,6]
19+
20+
输出:3
21+
```
22+
示例 2:
23+
![](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)
24+
```
25+
输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
26+
27+
输出:5
28+
```
29+
30+
提示:
31+
* 树的深度不会超过 1000 。
32+
* 树的节点数目位于 $[0, 10^4]$ 之间。
33+
34+
---
35+
36+
### DFS
37+
38+
根据题意,可以写出如下的 `DFS` 实现:从 $root$ 的所有子节点中的取最大深度,并在此基础上加一(统计 $root$ 节点)即是答案。
39+
40+
代码:
41+
```Java
42+
class Solution {
43+
public int maxDepth(Node root) {
44+
if (root == null) return 0;
45+
int ans = 0;
46+
for (Node node : root.children) {
47+
ans = Math.max(ans, maxDepth(node));
48+
}
49+
return ans + 1;
50+
}
51+
}
52+
```
53+
* 时间复杂度:$O(n)$
54+
* 空间复杂度:忽略递归带来的额外空间开销,复杂度为 $O(1)$
55+
56+
---
57+
58+
### BFS
59+
60+
同理,可以使用 `BFS` 进行求解:其本质是对多叉树进行层序处理,当 `BFS` 过程结束,意味着达到最大层数(深度),所记录的最大层数(深度)即是答案。
61+
62+
代码:
63+
```Java
64+
class Solution {
65+
public int maxDepth(Node root) {
66+
if (root == null) return 0;
67+
int ans = 0;
68+
Deque<Node> d = new ArrayDeque<>();
69+
d.addLast(root);
70+
while (!d.isEmpty()) {
71+
int size = d.size();
72+
while (size-- > 0) {
73+
Node t = d.pollFirst();
74+
for (Node node : t.children) {
75+
d.addLast(node);
76+
}
77+
}
78+
ans++;
79+
}
80+
return ans;
81+
}
82+
}
83+
```
84+
* 时间复杂度:$O(n)$
85+
* 空间复杂度:$O(n)$
86+
87+
---
88+
89+
### 最后
90+
91+
这是我们「刷穿 LeetCode」系列文章的第 `No.559` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
92+
93+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
94+
95+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode。
96+
97+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
98+

0 commit comments

Comments
 (0)