Skip to content

Commit c376e8e

Browse files
committed
0508 solved.
1 parent 7448101 commit c376e8e

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_0508)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0508 main.cpp)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/// Source : https://leetcode.com/problems/most-frequent-subtree-sum/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-18
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <map>
8+
9+
using namespace std;
10+
11+
12+
/// DFS
13+
/// Time Compelxity: O(n)
14+
/// Space Complexity: O(n)
15+
16+
/// Definition for a binary tree node.
17+
struct TreeNode {
18+
int val;
19+
TreeNode *left;
20+
TreeNode *right;
21+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
22+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
23+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
24+
};
25+
26+
class Solution {
27+
public:
28+
vector<int> findFrequentTreeSum(TreeNode* root) {
29+
30+
map<int, int> f;
31+
dfs(root, f);
32+
33+
vector<int> res;
34+
int max_f = 0;
35+
for(const pair<int, int>& p: f)
36+
if(p.second > max_f) res = {p.first}, max_f = p.second;
37+
else if(p.second == max_f) res.push_back(p.first);
38+
return res;
39+
}
40+
41+
private:
42+
int dfs(TreeNode* node, map<int, int>& f){
43+
44+
if(!node) return 0;
45+
46+
int res = node->val;
47+
res += dfs(node->left, f);
48+
res += dfs(node->right, f);
49+
f[res] ++;
50+
return res;
51+
}
52+
};
53+
54+
55+
int main() {
56+
57+
return 0;
58+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
537537
| 505 | [The Maze II](https://leetcode.com/problems/the-maze-ii/) | [solution](https://leetcode.com/problems/the-maze-ii/solution/) | [C++](0501-1000/0505-The-Maze-II/cpp-0505/) | | |
538538
| 506 | [Relative Ranks](https://leetcode.com/problems/relative-ranks/) | [] | [C++](0501-1000/0506-Relative-Ranks/cpp-0506/) | | |
539539
| 507 | [Perfect Number](https://leetcode.com/problems/perfect-number/) | [solution](https://leetcode.com/problems/perfect-number/solution/) | [C++](0501-1000/0507-Perfect-Number/cpp-0507/) | | |
540-
| | | | | | |
540+
| 508 | [Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/) | [] | [C++](0501-1000/0508-Most-Frequent-Subtree-Sum/cpp-0508/) | | |
541541
| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [] | [C++](0501-1000/0509-Fibonacci-Number/cpp-0509/) | | |
542542
| | | | | | |
543543
| 511 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - |

0 commit comments

Comments
 (0)