Children Sum Parent


Children Sum Parent – GeeksforGeeks


Problem: Children Sum Parent

Problem Statement

Given a binary tree, write a function that returns true if the tree follows the children sum property, else return false.

Children Sum Property:
For every node of the tree, the value of the node must be equal to the sum of the values of its left and right child.
For leaves, this condition is vacuously true.


Examples

Example 1:

        10
       /  \
      8    2
     / \
    3   5

Output: true
Explanation: 8 = 3 + 5 and 10 = 8 + 2

Example 2:

       10
      /  \
     20   30

Output: false
Explanation: 10 ≠ 20 + 30

Constraints


Intuition

The condition must be verified recursively at each node:


Approach

  1. Use post-order traversal (check children first).

  2. For each node:

    • If node is null or a leaf → return true

    • Recursively check left and right subtrees

    • If both pass, check if the current node's value equals the sum of child values.


Code (Java)

class Solution {
    public static int isSumProperty(Node root) {
        return check(root) ? 1 : 0;
    }

    private static boolean check(Node node) {
        if (node == null || (node.left == null && node.right == null)) {
            return true;
        }

        int leftVal = (node.left != null) ? node.left.data : 0;
        int rightVal = (node.right != null) ? node.right.data : 0;

        boolean currentNodeCheck = (node.data == leftVal + rightVal);
        return currentNodeCheck && check(node.left) && check(node.right);
    }
}

Time and Space Complexity


Dry Run

Input Tree:

        10
       /  \
      8    2
     / \
    3   5

Result: true


Conclusion