-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount Nodes Equal to Average of Subtree.cpp
31 lines (25 loc) · 1.42 KB
/
Count Nodes Equal to Average of Subtree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
int matchingSubtreeCount = 0; // Initialize the count of subtrees with matching averages.
// A Depth-First Search (DFS) function and returns a pair of values:
// - The sum of values within the current subtree.
// - The number of nodes within the current subtree.
pair<int, int> calculateSubtreeValues(TreeNode* currentNode) {
if (currentNode == nullptr)
return {0, 0}; // Base case: Return 0 for both sum and number of nodes if the node is null.
// Recursively calculate values for the left and right subtrees.
auto leftSubtree = calculateSubtreeValues(currentNode->left);
auto rightSubtree = calculateSubtreeValues(currentNode->right);
// Calculate the sum of values and the number of nodes in the current subtree.
int sumOfValues = leftSubtree.first + rightSubtree.first + currentNode->val;
int numberOfNodes = leftSubtree.second + rightSubtree.second + 1;
// Check if the current node's value matches the average of its subtree.
if (numberOfNodes != 0 && sumOfValues / numberOfNodes == currentNode->val)
matchingSubtreeCount++;
return {sumOfValues, numberOfNodes}; // Return the calculated values for the current subtree.
}
int averageOfSubtree(TreeNode* root) {
calculateSubtreeValues(root); // Start the DFS from the root node.
return matchingSubtreeCount;
}
};