Leetcode 508. Most Frequent Subtree Sum
Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order.
A subtree sum is defined as the sum of all the node values in a subtree.
Input: root = [5,2,-3]
Output: [2,-3,4]
Explanation:
5
/ \
2 -3
The subtree sums are 2, -3, and 4.
Input: root = [5,2,-5]
Output: [2]
Explanation:
5
/ \
2 -5
The subtree sums are 2, -5, and 2.
Assuming the function should handle negative values, not necessarily be sorted in any order if there’s a tie, and can handle up to typical tree node constraints (e.g., values between INT_MIN
and INT_MAX
, nodes count typical to competitive programming constraints).
Overall, the approach has a time complexity of O(N), where N is the number of nodes in the tree.
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <functional> // for std::function
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
vector<int> findFrequentTreeSum(TreeNode* root) {
if (!root) {
return {};
}
unordered_map<int, int> sumFrequencyMap;
int maxFrequency = 0;
function<int(TreeNode*)> dfs = [&](TreeNode* node) -> int {
if (!node) {
return 0;
}
int leftSum = dfs(node->left);
int rightSum = dfs(node->right);
int subtreeSum = node->val + leftSum + rightSum;
sumFrequencyMap[subtreeSum]++;
maxFrequency = max(maxFrequency, sumFrequencyMap[subtreeSum]);
return subtreeSum;
};
dfs(root);
vector<int> result;
for (const auto& pair : sumFrequencyMap) {
if (pair.second == maxFrequency) {
result.push_back(pair.first);
}
}
return result;
}
};
This code defines a Solution
class with a findFrequentTreeSum
function that takes the root of a binary tree and returns a vector containing the most frequent subtree sums. The code uses DFS to traverse the tree, calculates subtree sums, and maintains a frequency count of these sums to identify the most frequent ones.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?