Skip to content

Latest commit

 

History

History
60 lines (42 loc) · 1.11 KB

_1973. Count Nodes Equal to Sum of Descendants.md

File metadata and controls

60 lines (42 loc) · 1.11 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 23, 2024

Last updated : June 23, 2024


Related Topics : Tree, Depth-First Search, Binary Tree

Acceptance Rate : 77.19 %


Solutions

C

/**
 * 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;
}