root of a binary tree. For every node, compute its subtree total: the node's own value plus the values of all its descendants.
Among all these totals, find the ones that occur most often. If one total wins outright, return a list containing just it; if several share the top count, return all of them. The order of the answer does not matter.root=tree_node([3, 1, -4]) → [1, -4, 0]
The subtree totals are 1 for leaf 1, -4 for leaf -4, and 3 + 1 - 4 = 0 for the root. Each appears once, so all three are returned.
root=tree_node([6, 2, -2]) → [2, -2, 6]
The totals are 2, -2 and 6 + 2 - 2 = 6, again once each.
root=tree_node([4, 2, 6, None, None, 2]) → [2]
The two leaves give 2 and 2, node 6 with its left child 2 gives 8, and the root gives 4 + 2 + 8 = 14. The value 2 is the most frequent.
root=tree_node([3, 1, -4])
[1, -4, 0]
Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.