Children Sum in a Binary Tree

Last Updated : 19 Jul, 2026

Given a binary tree, find if it satisfies the Children Sum Property which has the following rules

  • Each non-leaf node must have a value equal to the sum of its left and right children's values.
  • A NULL child is considered to have a value of 0, and all leaf nodes are considered valid by default.

Examples:

Input: root = [35, 20, 15, 15, 5, 10, 5]

5

Output: true
Explanation: Here, every node is sum of its left and right child.

Input: root = [1, 4, 3, 5]

6

Output: false
Explanation: Here, 1 is the root node and 4, 3 are its child nodes. 4 + 3 = 7 which is not equal to the value of root node. Hence, this tree does not satisfy the given condition.

Try It Yourself
redirect icon

Using Recursive Validation - O(n) Time and O(h) Space

The idea is to use recursion to check the Children Sum Property while traversing the tree. For every node, we compare its value with the sum of its children.

Working of Approach:

  • Start from the root node. If the node is NULL or a leaf node, it automatically satisfies the Children Sum Property.
  • For every non-leaf node, compute the sum of the left and right child values (consider 0 for a missing child).
  • Compare this sum with the current node's value. If they are equal, recursively check the left and right subtrees.
  • If every node satisfies the condition, return true; otherwise, return false as soon as any node violates the property.

Let us understand with an example:
Input: root = [35, 20, 15, 15, 5, 10, 5]

5
  • Start from the root node (35) and check whether its value equals the sum of its children (20 + 15 = 35). Since it matches, recursively check both subtrees.
  • Move to node 20. Its children are 15 and 5, and 15 + 5 = 20, so continue checking its left and right child.
  • Nodes 15 and 5 are leaf nodes, so they automatically satisfy the Children Sum Property and return true.
  • Next, check node 15 on the right. Its children are 10 and 5, and 10 + 5 = 15, so both leaf nodes return true.
  • Since every non-leaf node satisfies the property, the function returns true.
C++
#include <bits/stdc++.h>
using namespace std;

class Node
{
  public:
    int data;
    Node *left, *right;

    Node(int val)
    {
        data = val;
        left = right = nullptr;
    }
};

bool isSumProperty(Node *root)
{
    int left_data = 0, right_data = 0;

    // if node is null or both child nodes are null, we return true.
    if (root == nullptr || (root->left == nullptr && root->right == nullptr))
        return true;

    else
    {

        // if left child is not null then we store its value.
        if (root->left != nullptr)
            left_data = root->left->data;

        // if right child is not null then we store its value.
        if (root->right != nullptr)
            right_data = root->right->data;

        // if sum of stored data of left and right child is equal to the current
        // node data and recursively for the left and right subtree, parent data
        // is equal to sum of child data then we return true.
        if ((root->data == left_data + right_data) && 
        isSumProperty(root->left) && isSumProperty(root->right))
            return true;

        // else we return false.
        else
            return false;
    }
}

int main()
{

    /*
             35
           /    \
         20      15
        /  \    /  \
      15    5  10   5
    */

    Node *root = new Node(35);
    root->left = new Node(20);
    root->right = new Node(15);

    root->left->left = new Node(15);
    root->left->right = new Node(5);

    root->right->left = new Node(10);
    root->right->right = new Node(5);

    if (isSumProperty(root))
        cout << "true";
    else
        cout << "false";

    return 0;
}
Java
class Node {
    int data;
    Node left, right;

    Node(int val)
    {
        data = val;
        left = right = null;
    }
}

class GFG {

    static boolean isSumProperty(Node root)
    {
        int left_data = 0, right_data = 0;

        // if node is null or both child nodes are null, we
        // return true.
        if (root == null
            || (root.left == null && root.right == null))
            return true;

        else {

            // if left child is not null then we store its
            // value.
            if (root.left != null)
                left_data = root.left.data;

            // if right child is not null then we store its
            // value.
            if (root.right != null)
                right_data = root.right.data;

            // if sum of stored data of left and right child
            // is equal to the current node data and
            // recursively for the left and right subtree,
            // parent data is equal to sum of child data
            // then we return true.
            if ((root.data == left_data + right_data)
                && isSumProperty(root.left)
                && isSumProperty(root.right))
                return true;

            // else we return false.
            else
                return false;
        }
    }

    public static void main(String[] args)
    {

        /*
                 35
               /    \
             20      15
            /  \    /  \
          15    5  10   5
        */

        Node root = new Node(35);
        root.left = new Node(20);
        root.right = new Node(15);

        root.left.left = new Node(15);
        root.left.right = new Node(5);

        root.right.left = new Node(10);
        root.right.right = new Node(5);

        if (isSumProperty(root))
            System.out.print("true");
        else
            System.out.print("false");
    }
}
Python
class Node:
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None


def isSumProperty(root):
    if root is None or (root.left is None and root.right is None):
        return True
    else:
        left_data = root.left.data if root.left is not None else 0
        right_data = root.right.data if root.right is not None else 0
        if (root.data == left_data + right_data) and isSumProperty(root.left) and isSumProperty(root.right):
            return True
        else:
            return False


if __name__ == '__main__':
    root = Node(35)
    root.left = Node(20)
    root.right = Node(15)
    root.left.left = Node(15)
    root.left.right = Node(5)
    root.right.left = Node(10)
    root.right.right = Node(5)
    if isSumProperty(root):
        print('true')
    else:
        print('false')
C#
using System;

class Node {
    public int data;
    public Node left, right;

    public Node(int val)
    {
        data = val;
        left = right = null;
    }
}

class GFG {
    static bool isSumProperty(Node root)
    {
        int left_data = 0, right_data = 0;

        // if node is null or both child nodes are null, we
        // return true.
        if (root == null
            || (root.left == null && root.right == null))
            return true;

        else {
            // if left child is not null then we store its
            // value.
            if (root.left != null)
                left_data = root.left.data;

            // if right child is not null then we store its
            // value.
            if (root.right != null)
                right_data = root.right.data;

            // if sum of stored data of left and right child
            // is equal to the current node data and
            // recursively for the left and right subtree,
            // parent data is equal to sum of child data
            // then we return true.
            if ((root.data == left_data + right_data)
                && isSumProperty(root.left)
                && isSumProperty(root.right))
                return true;

            // else we return false.
            else
                return false;
        }
    }

    static void Main()
    {
        /*
                 35
               /    \
             20      15
            /  \    /  \
          15    5  10   5
        */

        Node root = new Node(35);
        root.left = new Node(20);
        root.right = new Node(15);

        root.left.left = new Node(15);
        root.left.right = new Node(5);

        root.right.left = new Node(10);
        root.right.right = new Node(5);

        if (isSumProperty(root))
            Console.Write("true");
        else
            Console.Write("false");
    }
}
JavaScript
class Node {
    constructor(val)
    {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}

function isSumProperty(root)
{
    let left_data = 0, right_data = 0;

    // if node is null or both child nodes are null, we
    // return true.
    if (root === null
        || (root.left === null && root.right === null))
        return true;
    else {
        // if left child is not null then we store its
        // value.
        if (root.left !== null)
            left_data = root.left.data;

        // if right child is not null then we store its
        // value.
        if (root.right !== null)
            right_data = root.right.data;

        // if sum of stored data of left and right child is
        // equal to the current node data and recursively
        // for the left and right subtree, parent data is
        // equal to sum of child data then we return true.
        if ((root.data === left_data + right_data)
            && isSumProperty(root.left)
            && isSumProperty(root.right))
            return true;

        // else we return false.
        else
            return false;
    }
}

// Driver Code
let root = new Node(35);
root.left = new Node(20);
root.right = new Node(15);
root.left.left = new Node(15);
root.left.right = new Node(5);
root.right.left = new Node(10);
root.right.right = new Node(5);

if (isSumProperty(root))
    console.log("true");
else
    console.log("false");

Output
true
Comment