508. Most Frequent Subtree Sum

Given the root of a tree, you are asked to find the most frequent subtree sum. 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). So what is the most frequent subtree sum value? If there is a tie, return all the values with the highest frequency in any order.

Examples 1
Input:

```  5
/  \
2   -3
```
return [2, -3, 4], since all the values happen only once, return all of them in any order.

Examples 2
Input:

```  5
/  \
2   -5
```
return [2], since 2 happens twice, however -5 only occur once.

Note: You may assume the sum of values in any subtree is in the range of 32-bit signed integer.

Rust Solution

``````struct Solution;
use rustgym_util::*;
use std::collections::HashMap;

trait Postorder {
fn postorder(&self, hs: &mut HashMap<i32, usize>, max: &mut usize) -> i32;
}

fn postorder(&self, hs: &mut HashMap<i32, usize>, max: &mut usize) -> i32 {
if let Some(node) = self {
let node = node.borrow();
let val = node.val;
let left = node.left.postorder(hs, max);
let right = node.right.postorder(hs, max);
let sum = val + left + right;
let count = hs.entry(sum).or_default();
*count += 1;
*max = (*max).max(*count);
sum
} else {
0
}
}
}

impl Solution {
fn find_frequent_tree_sum(root: TreeLink) -> Vec<i32> {
let mut hs: HashMap<i32, usize> = HashMap::new();
let mut max: usize = 0;
root.postorder(&mut hs, &mut max);
hs.into_iter()
.filter_map(|(k, v)| if v == max { Some(k) } else { None })
.collect()
}
}

#[test]
fn test() {
let root = tree!(5, tree!(2), tree!(-3));
let mut res = vec![2, -3, 4];
let mut ans = Solution::find_frequent_tree_sum(root);
res.sort_unstable();
ans.sort_unstable();
assert_eq!(ans, res);
let root = tree!(5, tree!(2), tree!(-5));
let mut res = vec![2];
let mut ans = Solution::find_frequent_tree_sum(root);
res.sort_unstable();
ans.sort_unstable();
assert_eq!(ans, res);
}
``````

Having problems with this solution? Click here to submit an issue on github.