From 13c99397cf931519ba668280cc73bc8d52be34fd Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Mon, 5 Sep 2022 10:14:54 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20Add=20652?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Index/DFS.md | 1 + .../\344\272\214\345\217\211\346\240\221.md" | 1 + .../\345\223\210\345\270\214\350\241\250.md" | 1 + ...10\344\270\255\347\255\211\357\274\211.md" | 107 ++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 "LeetCode/651-660/652. \345\257\273\346\211\276\351\207\215\345\244\215\347\232\204\345\255\220\346\240\221\357\274\210\344\270\255\347\255\211\357\274\211.md" diff --git a/Index/DFS.md b/Index/DFS.md index 81f6e9c1..6c004a13 100644 --- a/Index/DFS.md +++ b/Index/DFS.md @@ -31,6 +31,7 @@ | [606. 根据二叉树创建字符串](https://leetcode-cn.com/problems/construct-string-from-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/construct-string-from-binary-tree/solution/by-ac_oier-i2sk/) | 简单 | 🤩🤩🤩🤩 | | [623. 在二叉树中增加一行](https://leetcode.cn/problems/add-one-row-to-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/add-one-row-to-tree/solution/by-ac_oier-sc34/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | +| [652. 寻找重复的子树](https://leetcode.cn/problems/find-duplicate-subtrees/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-duplicate-subtrees/solution/by-ac_oier-ly58/) | 中等 | 🤩🤩🤩🤩 | | [653. 两数之和 IV - 输入 BST](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/solution/by-ac_oier-zr4o/) | 简单 | 🤩🤩🤩🤩 | | [654. 最大二叉树](https://leetcode.cn/problems/maximum-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-binary-tree/solution/by-ac_oier-s0wc/) | 中等 | 🤩🤩🤩🤩🤩 | | [655. 输出二叉树](https://leetcode.cn/problems/print-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/print-binary-tree/solution/by-ac_oier-mays/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/Index/\344\272\214\345\217\211\346\240\221.md" "b/Index/\344\272\214\345\217\211\346\240\221.md" index a774821c..fa4abb66 100644 --- "a/Index/\344\272\214\345\217\211\346\240\221.md" +++ "b/Index/\344\272\214\345\217\211\346\240\221.md" @@ -9,6 +9,7 @@ | [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/) | 简单 | 🤩🤩🤩🤩 | | [606. 根据二叉树创建字符串](https://leetcode-cn.com/problems/construct-string-from-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/construct-string-from-binary-tree/solution/by-ac_oier-i2sk/) | 简单 | 🤩🤩🤩🤩 | | [623. 在二叉树中增加一行](https://leetcode.cn/problems/add-one-row-to-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/add-one-row-to-tree/solution/by-ac_oier-sc34/) | 中等 | 🤩🤩🤩🤩 | +| [652. 寻找重复的子树](https://leetcode.cn/problems/find-duplicate-subtrees/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-duplicate-subtrees/solution/by-ac_oier-ly58/) | 中等 | 🤩🤩🤩🤩 | | [653. 两数之和 IV - 输入 BST](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/solution/by-ac_oier-zr4o/) | 简单 | 🤩🤩🤩🤩 | | [654. 最大二叉树](https://leetcode.cn/problems/maximum-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-binary-tree/solution/by-ac_oier-s0wc/) | 中等 | 🤩🤩🤩🤩🤩 | | [655. 输出二叉树](https://leetcode.cn/problems/print-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/print-binary-tree/solution/by-ac_oier-mays/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/Index/\345\223\210\345\270\214\350\241\250.md" "b/Index/\345\223\210\345\270\214\350\241\250.md" index 1ab83b38..b84277d1 100644 --- "a/Index/\345\223\210\345\270\214\350\241\250.md" +++ "b/Index/\345\223\210\345\270\214\350\241\250.md" @@ -34,6 +34,7 @@ | [594. 最长和谐子序列](https://leetcode-cn.com/problems/longest-harmonious-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-harmonious-subsequence/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-quuh/) | 简单 | 🤩🤩🤩🤩 | | [599. 两个列表的最小索引总和](https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/solution/by-ac_oier-oh5b/) | 简单 | 🤩🤩🤩🤩 | | [645. 错误的集合](https://leetcode-cn.com/problems/set-mismatch/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/set-mismatch/solution/gong-shui-san-xie-yi-ti-san-jie-ji-shu-s-vnr9/) | 简单 | 🤩🤩🤩 | +| [652. 寻找重复的子树](https://leetcode.cn/problems/find-duplicate-subtrees/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-duplicate-subtrees/solution/by-ac_oier-ly58/) | 中等 | 🤩🤩🤩🤩 | | [653. 两数之和 IV - 输入 BST](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/solution/by-ac_oier-zr4o/) | 简单 | 🤩🤩🤩🤩 | | [662. 二叉树最大宽度](https://leetcode.cn/problems/maximum-width-of-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-width-of-binary-tree/solution/by-ac_oier-33er/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/651-660/652. \345\257\273\346\211\276\351\207\215\345\244\215\347\232\204\345\255\220\346\240\221\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/651-660/652. \345\257\273\346\211\276\351\207\215\345\244\215\347\232\204\345\255\220\346\240\221\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..1ed8e401 --- /dev/null +++ "b/LeetCode/651-660/652. \345\257\273\346\211\276\351\207\215\345\244\215\347\232\204\345\255\220\346\240\221\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,107 @@ +### 题目描述 + +这是 LeetCode 上的 **[652. 寻找重复的子树](https://leetcode.cn/problems/find-duplicate-subtrees/solution/by-ac_oier-ly58/)** ,难度为 **中等**。 + +Tag : 「二叉树」、「哈希表」、「DFS」 + + + +给定一棵二叉树 `root`,返回所有重复的子树。 + +对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可。 + +如果两棵树具有相同的结构和相同的结点值,则它们是重复的。 + +示例 1: +![](https://assets.leetcode.com/uploads/2020/08/16/e1.jpg) +``` +输入:root = [1,2,3,4,null,2,4,null,null,4] + +输出:[[2,4],[4]] +``` +示例 2: +![](https://assets.leetcode.com/uploads/2020/08/16/e2.jpg) +``` +输入:root = [2,1,1] + +输出:[[1]] +``` +示例 3: +![](https://assets.leetcode.com/uploads/2020/08/16/e33.jpg) +``` +输入:root = [2,2,2,3,null,3,null] + +输出:[[2,3],[3]] +``` + +提示: +* 树中的结点数在 $[1,10^4]$ 范围内。 +* $-200 <= Node.val <= 200$ + +--- + +### DFS + 哈希表 + +设计递归函数 `String dfs(TreeNode root)`,含义为返回以传入参数 `root` 为根节点的子树所对应的指纹标识。 + +对于标识的设计只需使用 `"_"` 分割不同的节点值,同时对空节点进行保留(定义为空串 `" "`)即可。 + +使用哈希表记录每个标识(子树)出现次数,当出现次数为 $2$(首次判定为重复出现)时,将该节点加入答案。 + +Java 代码: +```Java +class Solution { + Map map = new HashMap<>(); + List ans = new ArrayList<>(); + public List findDuplicateSubtrees(TreeNode root) { + dfs(root); + return ans; + } + String dfs(TreeNode root) { + if (root == null) return " "; + StringBuilder sb = new StringBuilder(); + sb.append(root.val).append("_"); + sb.append(dfs(root.left)).append(dfs(root.right)); + String key = sb.toString(); + map.put(key, map.getOrDefault(key, 0) + 1); + if (map.get(key) == 2) ans.add(root); + return key; + } +} +``` +Typescript 代码: +```Typescript +let ans: Array +let map: Map +function findDuplicateSubtrees(root: TreeNode | null): Array { + ans = new Array() + map = new Map() + dfs(root) + return ans +}; +function dfs(root: TreeNode | null): string { + if (root == null) return " " + let key = root.val + "_" + key += dfs(root.left) + key += dfs(root.right) + if (!map.has(key)) map.set(key, 1) + else map.set(key, map.get(key) + 1) + if (map.get(key) == 2) ans.push(root) + return key +} +``` +* 时间复杂度:`DFS` 过程复杂度为 $O(n)$,对于每个子树需要构造出与子树同等规模的字符串,复杂度为 $O(n)$。整体复杂度为 $O(n^2)$ +* 空间复杂度:$O(n)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.652` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +