Skip to content

Commit 20c74fe

Browse files
Merge pull request SharingSource#566 from SharingSource/ac_oier
✨feat: add 508
2 parents d5aff5d + 2ed9172 commit 20c74fe

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

Index/DFS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
| [473. 火柴拼正方形](https://leetcode.cn/problems/matchsticks-to-square/) | [LeetCode 题解链接](https://leetcode.cn/problems/matchsticks-to-square/solution/by-ac_oier-k8i7/) | 中等 | 🤩🤩🤩🤩 |
2020
| [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/) | 困难 | 🤩🤩🤩🤩 |
2121
| [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/) | 中等 | 🤩🤩🤩🤩 |
22+
| [508. 出现次数最多的子树元素和](https://leetcode.cn/problems/most-frequent-subtree-sum/) | [LeetCode 题解链接](https://leetcode.cn/problems/most-frequent-subtree-sum/solution/by-ac_oier-t3y4/) | 中等 | 🤩🤩🤩 |
2223
| [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/) | 简单 | 🤩🤩🤩🤩 |
2324
| [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/) | 简单 | 🤩🤩🤩🤩 |
2425
| [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/) | 简单 | 🤩🤩🤩 |

Index/哈希表.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
| [460. LFU 缓存](https://leetcode-cn.com/problems/lfu-cache/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lfu-cache/solution/gong-shui-san-xie-yun-yong-tong-pai-xu-s-53m3/) | 困难 | 🤩🤩🤩🤩🤩 |
2626
| [496. 下一个更大元素 I](https://leetcode-cn.com/problems/next-greater-element-i/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/next-greater-element-i/solution/gong-shui-san-xie-yi-ti-shuang-jie-bian-n6nwz/) | 中等 | 🤩🤩🤩🤩 |
2727
| [500. 键盘行](https://leetcode-cn.com/problems/keyboard-row/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/keyboard-row/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-zx6b/) | 简单 | 🤩🤩🤩🤩 |
28+
| [508. 出现次数最多的子树元素和](https://leetcode.cn/problems/most-frequent-subtree-sum/) | [LeetCode 题解链接](https://leetcode.cn/problems/most-frequent-subtree-sum/solution/by-ac_oier-t3y4/) | 中等 | 🤩🤩🤩 |
2829
| [519. 随机翻转矩阵](https://leetcode-cn.com/problems/random-flip-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/random-flip-matrix/solution/gong-shui-san-xie-note-bie-pian-yi-ti-sh-e6gi/) | 中等 | 🤩🤩🤩🤩 |
2930
| [532. 数组中的 k-diff 数对](https://leetcode.cn/problems/k-diff-pairs-in-an-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/k-diff-pairs-in-an-array/solution/by-ac_oier-ap3v/) | 中等 | 🤩🤩🤩🤩 |
3031
| [554. 砖墙](https://leetcode-cn.com/problems/brick-wall/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/brick-wall/solution/gong-shui-san-xie-zheng-nan-ze-fan-shi-y-gsri/) | 中等 | 🤩🤩🤩 |
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[508. 出现次数最多的子树元素和](https://leetcode.cn/problems/most-frequent-subtree-sum/solution/by-ac_oier-t3y4/)** ,难度为 **中等**
4+
5+
Tag : 「树的遍历」、「DFS」、「哈希表」
6+
7+
8+
9+
给你一个二叉树的根结点 `root`,请返回出现次数最多的子树元素和。如果有多个元素出现的次数相同,返回所有出现次数最多的子树元素和(不限顺序)。
10+
11+
一个结点的 「子树元素和」 定义为以该结点为根的二叉树上所有结点的元素之和(包括结点本身)。
12+
13+
示例 1:
14+
![](https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg)
15+
```
16+
输入: root = [5,2,-3]
17+
18+
输出: [2,-3,4]
19+
```
20+
示例 2:
21+
![](https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg)
22+
```
23+
输入: root = [5,2,-5]
24+
25+
输出: [2]
26+
```
27+
28+
提示:
29+
* 节点数在 $[1, 10^4]$ 范围内
30+
* $-10^5 <= Node.val <= 10^5$
31+
32+
---
33+
34+
### DFS
35+
36+
使用 `DFS` 对树进行遍历,同时将每棵子树的总和值存入哈希表(以元素和为 `key`,出现次数为 `value`),并实时维护最大子树和,使用变量 `max` 存储。
37+
38+
当遍历完整棵树后,将哈希表中值为 `max` 的键存入答案。
39+
40+
代码:
41+
```Java
42+
class Solution {
43+
Map<Integer, Integer> map = new HashMap<>();
44+
int max = 0;
45+
public int[] findFrequentTreeSum(TreeNode root) {
46+
dfs(root);
47+
List<Integer> list = new ArrayList<>();
48+
for (int k : map.keySet()) {
49+
if (map.get(k) == max) list.add(k);
50+
}
51+
int n = list.size();
52+
int[] ans = new int[n];
53+
for (int i = 0; i < n; i++) ans[i] = list.get(i);
54+
return ans;
55+
}
56+
int dfs(TreeNode root) {
57+
if (root == null) return 0;
58+
int cur = root.val + dfs(root.left) + dfs(root.right);
59+
map.put(cur, map.getOrDefault(cur, 0) + 1);
60+
max = Math.max(max, map.get(cur));
61+
return cur;
62+
}
63+
}
64+
```
65+
* 时间复杂度:$O(n)$
66+
* 空间复杂度:$O(n)$
67+
68+
---
69+
70+
### 最后
71+
72+
这是我们「刷穿 LeetCode」系列文章的第 `No.508` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
73+
74+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
75+
76+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
77+
78+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
79+

0 commit comments

Comments
 (0)