Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 38dfdc1

Browse files
Merge pull request SharingSource#405 from SharingSource/ac_oier
✨feat: Add 590
2 parents 9478ef0 + 065c238 commit 38dfdc1

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed

‎Index/BFS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
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/) | 困难 | 🤩🤩🤩🤩 |
77
| [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/) | 简单 | 🤩🤩🤩🤩 |
8+
| [589. N 叉树的前序遍历](https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/solution/gong-shui-san-xie-shu-de-sou-suo-yun-yon-pse1/) | 简单 | 🤩🤩🤩 |
9+
| [590. N 叉树的后序遍历](https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/solution/by-ac_oier-ul7t/) | 简单 | 🤩🤩🤩 |
810
| [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/) | 简单 | 🤩🤩🤩 |
911
| [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/) | 困难 | 🤩🤩🤩 |
1012
| [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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
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/) | 中等 | 🤩🤩🤩🤩 |
1717
| [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/) | 简单 | 🤩🤩🤩🤩 |
1818
| [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/) | 简单 | 🤩🤩🤩🤩 |
19+
| [589. N 叉树的前序遍历](https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/solution/gong-shui-san-xie-shu-de-sou-suo-yun-yon-pse1/) | 简单 | 🤩🤩🤩 |
20+
| [590. N 叉树的后序遍历](https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/solution/by-ac_oier-ul7t/) | 简单 | 🤩🤩🤩 |
1921
| [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/) | 中等 | 🤩🤩🤩🤩 |
2022
| [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/) | 中等 | 🤩🤩🤩🤩 |
2123
| [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/) | 简单 | 🤩🤩🤩 |

‎Index/树的搜索.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| [173. 二叉搜索树迭代器](https://leetcode-cn.com/problems/binary-search-tree-iterator/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-search-tree-iterator/solution/xiang-jie-ru-he-dui-die-dai-ban-de-zhong-4rxj/) | 中等 | 🤩🤩🤩🤩 |
55
| [331. 验证二叉树的前序序列化](https://leetcode-cn.com/problems/verify-preorder-serialization-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/verify-preorder-serialization-of-a-binary-tree/solution/xiang-xin-ke-xue-xi-lie-xiang-jie-zhi-gu-e3y9/) | 中等 | 🤩🤩🤩 |
66
| [589. N 叉树的前序遍历](https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/solution/gong-shui-san-xie-shu-de-sou-suo-yun-yon-pse1/) | 简单 | 🤩🤩🤩 |
7+
| [590. N 叉树的后序遍历](https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/solution/by-ac_oier-ul7t/) | 简单 | 🤩🤩🤩 |
78
| [671. 二叉树中第二小的节点](https://leetcode-cn.com/problems/second-minimum-node-in-a-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/second-minimum-node-in-a-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-shu-d-eupu/) | 简单 | 🤩🤩 |
89
| [700. 二叉搜索树中的搜索](https://leetcode-cn.com/problems/search-in-a-binary-search-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/search-in-a-binary-search-tree/solution/gong-shui-san-xie-er-cha-shu-de-sou-suo-8z7hj/) | 简单 | 🤩🤩🤩🤩 |
910
| [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/) | 困难 | 🤩🤩🤩 |
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[507. 完美数](https://leetcode-cn.com/problems/perfect-number/solution/gong-shui-san-xie-jian-dan-mo-ni-tong-ji-e6jk/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「数论」、「数学」
6+
7+
8+
9+
对于一个 正整数,如果它和除了它自身以外的所有 **正因子** 之和相等,我们称它为 「完美数」。
10+
11+
给定一个 整数 `n`, 如果是完美数,返回 `true`,否则返回 `false`
12+
13+
示例 1:
14+
```
15+
输入:num = 28
16+
17+
输出:true
18+
19+
解释:28 = 1 +たす 2 +たす 4 +たす 7 +たす 14
20+
1, 2, 4, 7, 和 14 是 28 的所有正因子。
21+
```
22+
示例 2:
23+
```
24+
输入:num = 6
25+
26+
输出:true
27+
```
28+
示例 3:
29+
```
30+
输入:num = 496
31+
32+
输出:true
33+
```
34+
示例 4:
35+
```
36+
输入:num = 8128
37+
38+
输出:true
39+
```
40+
示例 5:
41+
```
42+
输入:num = 2
43+
44+
输出:false
45+
```
46+
47+
提示:
48+
* 1ドル <= num <= 10^8$
49+
50+
---
51+
52+
### 数学
53+
54+
我们知道正因数总是成对的出现,因此我们可以仅枚举每对正因数的较小数,即从 $[1, \sqrt{num}]$ 范围内进行枚举(其中 $nums > 1$)。
55+
56+
同时为避免使用 `sqrt` 库函数和溢出,使用 $i <= \frac{num}{i}$ 作为上界判断。
57+
58+
代码:
59+
```Java
60+
class Solution {
61+
public boolean checkPerfectNumber(int num) {
62+
if (num == 1) return false;
63+
int ans = 1;
64+
for (int i = 2; i <= num / i; i++) {
65+
if (num % i == 0) {
66+
ans += i;
67+
if (i * i != num) ans += num / i;
68+
}
69+
}
70+
return ans == num;
71+
}
72+
}
73+
```
74+
* 时间复杂度:$O(\sqrt{num})$
75+
* 空间复杂度:$O(1)$
76+
77+
---
78+
79+
### 最后
80+
81+
这是我们「刷穿 LeetCode」系列文章的第 `No.507` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
82+
83+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
84+
85+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
86+
87+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
88+
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[590. N 叉树的后序遍历(简单)](https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/solution/by-ac_oier-ul7t/)** ,难度为 **简单**
4+
5+
Tag : 「递归」、「迭代」、「非递归」、「DFS」、「BFS」
6+
7+
8+
9+
给定一个 $n$ 叉树的根节点 $root$ ,返回 其节点值的 **后序遍历**
10+
11+
$n$ 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 `null` 分隔(请参见示例)。
12+
13+
示例 1:
14+
![](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)
15+
```
16+
输入:root = [1,null,3,2,4,null,5,6]
17+
18+
输出:[5,6,3,2,4,1]
19+
```
20+
示例 2:
21+
![](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)
22+
```
23+
输入: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]
24+
25+
输出:[2,6,14,11,7,3,12,8,4,13,9,10,5,1]
26+
```
27+
28+
提示:
29+
* 节点总数在范围 $[0, 10^4]$ 内
30+
* 0ドル <= Node.val <= 10^4$
31+
* $n$ 叉树的高度小于或等于 1000ドル$
32+
33+
进阶:递归法很简单,你可以使用迭代法完成此题吗?
34+
35+
---
36+
37+
### 递归
38+
39+
常规做法,不再赘述。
40+
41+
代码:
42+
```Java
43+
class Solution {
44+
List<Integer> ans = new ArrayList<>();
45+
public List<Integer> postorder(Node root) {
46+
dfs(root);
47+
return ans;
48+
}
49+
void dfs(Node root) {
50+
if (root == null) return;
51+
for (Node node : root.children) dfs(node);
52+
ans.add(root.val);
53+
}
54+
}
55+
```
56+
* 时间复杂度:$O(n)$
57+
* 空间复杂度:忽略递归带来的额外空间开销,复杂度为 $O(1)$
58+
59+
---
60+
61+
### 非递归
62+
63+
针对本题,使用「栈」模拟递归过程。
64+
65+
迭代过程中记录 `(cnt = 当前节点遍历过的子节点数量, node = 当前节点)` 二元组,每次取出栈顶元素,如果当前节点已经遍历完所有的子节点(当前遍历过的子节点数量为 $cnt = 子节点数量$),则将当前节点的值加入答案。
66+
67+
否则更新当前元素遍历过的子节点数量,并重新入队,即将 $(cnt + 1, node)$ 入队,以及将下一子节点 $(0, node.children[cnt])$ 进行首次入队。
68+
69+
70+
代码:
71+
```Java
72+
class Solution {
73+
public List<Integer> postorder(Node root) {
74+
List<Integer> ans = new ArrayList<>();
75+
Deque<Object[]> d = new ArrayDeque<>();
76+
d.addLast(new Object[]{0, root});
77+
while (!d.isEmpty()) {
78+
Object[] poll = d.pollLast();
79+
Integer cnt = (Integer)poll[0]; Node t = (Node)poll[1];
80+
if (t == null) continue;
81+
if (cnt == t.children.size()) ans.add(t.val);
82+
if (cnt < t.children.size()) {
83+
d.addLast(new Object[]{cnt + 1, t});
84+
d.addLast(new Object[]{0, t.children.get(cnt)});
85+
}
86+
}
87+
return ans;
88+
}
89+
}
90+
```
91+
* 时间复杂度:$O(n)$
92+
* 空间复杂度:$O(n)$
93+
94+
---
95+
96+
### 通用「非递归」
97+
98+
另外一种「递归」转「迭代」的做法,是直接模拟系统执行「递归」的过程,这是一种更为通用的做法。
99+
100+
**由于现代编译器已经做了很多关于递归的优化,现在这种技巧已经无须掌握。**
101+
102+
在迭代过程中记录当前栈帧位置状态 `loc`,在每个状态流转节点做相应操作。
103+
104+
代码:
105+
```Java
106+
class Solution {
107+
public List<Integer> postorder(Node root) {
108+
List<Integer> ans = new ArrayList<>();
109+
Deque<Object[]> d = new ArrayDeque<>();
110+
d.addLast(new Object[]{0, root});
111+
while (!d.isEmpty()) {
112+
Object[] poll = d.pollLast();
113+
Integer loc = (Integer)poll[0]; Node t = (Node)poll[1];
114+
if (t == null) continue;
115+
if (loc == 0) {
116+
d.addLast(new Object[]{1, t});
117+
int n = t.children.size();
118+
for (int i = n - 1; i >= 0; i--) d.addLast(new Object[]{0, t.children.get(i)});
119+
} else if (loc == 1) {
120+
ans.add(t.val);
121+
}
122+
}
123+
return ans;
124+
}
125+
}
126+
```
127+
* 时间复杂度:$O(n)$
128+
* 空间复杂度:$O(n)$
129+
130+
---
131+
132+
### 最后
133+
134+
这是我们「刷穿 LeetCode」系列文章的第 `No.590` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
135+
136+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
137+
138+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
139+
140+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
141+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /