diff --git a/Index/BFS.md b/Index/BFS.md index 2e0a6c48..96e1b841 100644 --- a/Index/BFS.md +++ b/Index/BFS.md @@ -5,6 +5,8 @@ | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | diff --git a/Index/DFS.md b/Index/DFS.md index dee711a1..f552e7c0 100644 --- a/Index/DFS.md +++ b/Index/DFS.md @@ -16,6 +16,8 @@ | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | diff --git "a/Index/\346\240\221\347\232\204\346\220\234\347\264\242.md" "b/Index/\346\240\221\347\232\204\346\220\234\347\264\242.md" index b202b81c..363fda7f 100644 --- "a/Index/\346\240\221\347\232\204\346\220\234\347\264\242.md" +++ "b/Index/\346\240\221\347\232\204\346\220\234\347\264\242.md" @@ -4,6 +4,7 @@ | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩 | diff --git "a/LeetCode/501-510/590. N \345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/501-510/590. N \345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..b6c4360b --- /dev/null +++ "b/LeetCode/501-510/590. N \345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,88 @@ +### 题目描述 + +这是 LeetCode 上的 **[507. 完美数](https://leetcode-cn.com/problems/perfect-number/solution/gong-shui-san-xie-jian-dan-mo-ni-tong-ji-e6jk/)** ,难度为 **简单**。 + +Tag : 「模拟」、「数论」、「数学」 + + + +对于一个 正整数,如果它和除了它自身以外的所有 **正因子** 之和相等,我们称它为 「完美数」。 + +给定一个 整数 `n`, 如果是完美数,返回 `true`,否则返回 `false`。 + +示例 1: +``` +输入:num = 28 + +输出:true + +解释:28 = 1 + 2 + 4 + 7 + 14 +1, 2, 4, 7, 和 14 是 28 的所有正因子。 +``` +示例 2: +``` +输入:num = 6 + +输出:true +``` +示例 3: +``` +输入:num = 496 + +输出:true +``` +示例 4: +``` +输入:num = 8128 + +输出:true +``` +示例 5: +``` +输入:num = 2 + +输出:false +``` + +提示: +* $1 <= num <= 10^8$ + +--- + +### 数学 + +我们知道正因数总是成对的出现,因此我们可以仅枚举每对正因数的较小数,即从 $[1, \sqrt{num}]$ 范围内进行枚举(其中 $nums > 1$)。 + +同时为避免使用 `sqrt` 库函数和溢出,使用 $i <= \frac{num}{i}$ 作为上界判断。 + +代码: +```Java +class Solution { + public boolean checkPerfectNumber(int num) { + if (num == 1) return false; + int ans = 1; + for (int i = 2; i <= num / i; i++) { + if (num % i == 0) { + ans += i; + if (i * i != num) ans += num / i; + } + } + return ans == num; + } +} +``` +* 时间复杂度:$O(\sqrt{num})$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.507` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 + diff --git "a/LeetCode/581-590/590. N \345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/581-590/590. N \345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..4fe43be7 --- /dev/null +++ "b/LeetCode/581-590/590. N \345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,141 @@ +### 题目描述 + +这是 LeetCode 上的 **[590. N 叉树的后序遍历(简单)](https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/solution/by-ac_oier-ul7t/)** ,难度为 **简单**。 + +Tag : 「递归」、「迭代」、「非递归」、「DFS」、「BFS」 + + + +给定一个 $n$ 叉树的根节点 $root$ ,返回 其节点值的 **后序遍历** 。 + +$n$ 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 `null` 分隔(请参见示例)。 + +示例 1: +![](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png) +``` +输入:root = [1,null,3,2,4,null,5,6] + +输出:[5,6,3,2,4,1] +``` +示例 2: +![](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png) +```  +输入: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] + +输出:[2,6,14,11,7,3,12,8,4,13,9,10,5,1] +``` + +提示: +* 节点总数在范围 $[0, 10^4]$ 内 +* $0 <= Node.val <= 10^4$ +* $n$ 叉树的高度小于或等于 $1000$ + +进阶:递归法很简单,你可以使用迭代法完成此题吗? + +--- + +### 递归 + +常规做法,不再赘述。 + +代码: +```Java +class Solution { + List ans = new ArrayList<>(); + public List postorder(Node root) { + dfs(root); + return ans; + } + void dfs(Node root) { + if (root == null) return; + for (Node node : root.children) dfs(node); + ans.add(root.val); + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:忽略递归带来的额外空间开销,复杂度为 $O(1)$ + +--- + +### 非递归 + +针对本题,使用「栈」模拟递归过程。 + +迭代过程中记录 `(cnt = 当前节点遍历过的子节点数量, node = 当前节点)` 二元组,每次取出栈顶元素,如果当前节点已经遍历完所有的子节点(当前遍历过的子节点数量为 $cnt = 子节点数量$),则将当前节点的值加入答案。 + +否则更新当前元素遍历过的子节点数量,并重新入队,即将 $(cnt + 1, node)$ 入队,以及将下一子节点 $(0, node.children[cnt])$ 进行首次入队。 + + +代码: +```Java +class Solution { + public List postorder(Node root) { + List ans = new ArrayList<>(); + Deque d = new ArrayDeque<>(); + d.addLast(new Object[]{0, root}); + while (!d.isEmpty()) { + Object[] poll = d.pollLast(); + Integer cnt = (Integer)poll[0]; Node t = (Node)poll[1]; + if (t == null) continue; + if (cnt == t.children.size()) ans.add(t.val); + if (cnt < t.children.size()) { + d.addLast(new Object[]{cnt + 1, t}); + d.addLast(new Object[]{0, t.children.get(cnt)}); + } + } + return ans; + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(n)$ + +--- + +### 通用「非递归」 + +另外一种「递归」转「迭代」的做法,是直接模拟系统执行「递归」的过程,这是一种更为通用的做法。 + +**由于现代编译器已经做了很多关于递归的优化,现在这种技巧已经无须掌握。** + +在迭代过程中记录当前栈帧位置状态 `loc`,在每个状态流转节点做相应操作。 + +代码: +```Java +class Solution { + public List postorder(Node root) { + List ans = new ArrayList<>(); + Deque d = new ArrayDeque<>(); + d.addLast(new Object[]{0, root}); + while (!d.isEmpty()) { + Object[] poll = d.pollLast(); + Integer loc = (Integer)poll[0]; Node t = (Node)poll[1]; + if (t == null) continue; + if (loc == 0) { + d.addLast(new Object[]{1, t}); + int n = t.children.size(); + for (int i = n - 1; i >= 0; i--) d.addLast(new Object[]{0, t.children.get(i)}); + } else if (loc == 1) { + ans.add(t.val); + } + } + return ans; + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(n)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.590` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +