1234567891011121314151617181920212223242526272829303132333435 |
- var max int = 0
- /**
- * Definition for a binary tree node.
- * type TreeNode struct {
- * Val int
- * Left *TreeNode
- * Right *TreeNode
- * }
- */
- func findFrequentTreeSum(root *TreeNode) (res []int) {
- max = 0
- freq := make(map[int]int)
- postorder(root, &freq)
- for k, v := range freq {
- if v == max {
- res = append(res, k)
- }
- }
- return
- }
- func postorder(root *TreeNode, freq *map[int]int) int {
- if root == nil {
- return 0
- }
- l := postorder(root.Left, freq)
- r := postorder(root.Right, freq)
- sum := l + r + root.Val
- (*freq)[sum]++
- if max < (*freq)[sum] {
- max = (*freq)[sum]
- }
- return sum
- }
|