Most Frequent Subtree Sum
You are given the root of a binary tree. The subtree sum of a node is the sum of the values of the node and all its descendants. Find the subtree sum that occurs most often among all nodes. If several sums tie for the most occurrences, return all of them. The order of the returned list does not matter. An empty tree gives [].
Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.
Example 1
Sums: 2, -3 and 4. Each occurs once, so all three are returned.
Example 2
Sums: 2, -5 and 2. The sum 2 occurs twice.
Constraints
0 <= number of nodes <= 100-100 <= node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.