All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 23, 2024
Last updated : June 23, 2024
Related Topics : Tree, Depth-First Search, Binary Tree
Acceptance Rate : 77.19 %
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int output;
long helper(struct TreeNode* root) {
if (!root) {
return 0;
}
long sum = helper(root->left) + helper(root->right);
if (root->val == sum) {
output++;
}
return sum + root->val;
}
int equalToDescendants(struct TreeNode* root) {
output = 0;
helper(root);
return output;
}