-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path508-MostFrequentSubtreeSum.go
More file actions
101 lines (92 loc) · 2.44 KB
/
508-MostFrequentSubtreeSum.go
File metadata and controls
101 lines (92 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
// 508. Most Frequent Subtree Sum
// Given the root of a binary tree, return the most frequent subtree sum.
// If there is a tie, return all the values with the highest frequency in any order.
// The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).
// Example 1:
// 5
// / \
// 2 -3
// <img src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" />
// Input: root = [5,2,-3]
// Output: [2,-3,4]
// Example 2:
// 5
// / \
// 2 -5
// <img src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" />
// Input: root = [5,2,-5]
// Output: [2]
// Constraints:
// The number of nodes in the tree is in the range [1, 10^4].
// -10^5 <= Node.val <= 10^5
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func findFrequentTreeSum(root *TreeNode) []int {
res, mx, mp := []int{}, 0, make(map[int]int)
var dfs func(root *TreeNode) int
dfs = func(root *TreeNode) int {
if root == nil {
return 0
}
curr := root.Val + dfs(root.Left) + dfs(root.Right)
mp[curr]++
if mp[curr] > mx {
mx = mp[curr]
}
return curr
}
dfs(root)
// for _, v:= range mp { // 找到最大值
// if v > mx {
// mx = v
// }
// }
for k, v := range mp {
if v == mx {
res = append(res, k)
}
}
return res
}
func main() {
// Example 1:
// 5
// / \
// 2 -3
// <img src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" />
// Input: root = [5,2,-3]
// Output: [2,-3,4]
tree1 := &TreeNode {
5,
&TreeNode{2, nil, nil},
&TreeNode{-3, nil, nil},
}
fmt.Println(findFrequentTreeSum(tree1)) // [2,-3,4]
// Example 2:
// 5
// / \
// 2 -5
// <img src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" />
// Input: root = [5,2,-5]
// Output: [2]
tree2 := &TreeNode {
5,
&TreeNode{2, nil, nil},
&TreeNode{-5, nil, nil},
}
fmt.Println(findFrequentTreeSum(tree2)) // [2]
}