#508

Most Frequent Subtree Sum

pupil · 585 · lc medium +29 · verified · 69% accepted · 2,369 likes · top 76%

play →

Description

Given the root of a binary tree, compute the subtree sum for every node (the total of all values in the subtree rooted at that node, including itself). Return the subtree sum value(s) that appear most frequently. If multiple values share the top frequency, return all of them in any order.

Example 1:

Input: root = [5,2,-3]
Output: [2,-3,4]

Example 2:

Input: root = [5,2,-5]
Output: [2]

Code

1
2
3
4
5
6
7
8
9