Check if a given Binary Tree is Sum Tree

Last Updated : 14 Jul, 2026

Given the root of a Binary Tree with n nodes, check whether it is a Sum Tree and return true if it is, otherwise return false.

A Sum Tree is a Binary Tree in which the value of every non-leaf node is equal to the sum of all nodes present in its left and right subtrees. An empty tree and a leaf node are also considered Sum Trees.

Example:

Input: root[] = [3, 1, 2]
Output: true
Explanation: The sum of left subtree and right subtree is 1 + 2 = 3, which is the value of the root node. Therefore, the given binary tree is a sum tree.

split_array_into_three_equal_sum_segments_42

Input: root[] = [10, 20, 30, 10, 10]
Output: false
Explanation: The given tree is not a Sum Tree. For the root node, the sum of nodes in the left and right subtrees is 40 + 30 = 70, which is not equal to the root value 10.

split_array_into_three_equal_sum_segments_41
Try It Yourself
redirect icon

[Naive Approach] By Checking Every Node - O(n^2) Time and O(h) Space

The idea is to get the sum of the left subtree and right subtree for each node and compare it with the node's value. Also recursively check if the left and right subtree are sum trees or not.

  • If the current node is NULL or a leaf node, return true since it is a Sum Tree.
  • Recursively compute the sum of all nodes in the left subtree.
  • Recursively compute the sum of all nodes in the right subtree.
  • Check whether the current node's value equals the sum of the left and right subtree sums.
  • Recursively verify that both the left and right subtrees are also Sum Trees.
  • Return true only if the current node satisfies the Sum Tree property and both subtrees are Sum Trees; otherwise, return false.
C++
#include <iostream>
using namespace std;

class Node
{
  public:
    int data;
    Node *left, *right;
    Node(int x)
    {
        data = x;
        left = right = nullptr;
    }
};
// Returns the sum of all nodes in the subtree rooted at 'root'.
int sum(Node *root)
{
    if (root == nullptr)
        return 0;
    return root->data + sum(root->left) + sum(root->right);
}
// Returns true if the binary tree rooted at 'root' is a Sum Tree.
bool isSumTree(Node *root)
{
    // An empty tree and a leaf node are always Sum Trees.
    if (root == nullptr || (root->left == nullptr && root->right == nullptr))
        return true;
    // Calculate the sum of nodes in the left and right subtrees.
    int leftSum = sum(root->left);
    int rightSum = sum(root->right);
    // Check the Sum Tree property for the current node and
    // recursively verify both subtrees.
    return (root->data == leftSum + rightSum) && isSumTree(root->left) && isSumTree(root->right);
}
int main()
{
    // Create the following binary tree:
    //
    //       3
    //      / \
    //     1   2
    //
    Node *root = new Node(3);
    root->left = new Node(1);
    root->right = new Node(2);
    cout << (isSumTree(root) ? "true" : "false");
    return 0;
}
C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
} Node;

// Creates a new tree node.
Node *createNode(int data)
{
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}

// Returns the sum of all nodes in the subtree rooted at 'root'.
int sum(Node *root)
{
    if (root == NULL)
        return 0;

    return root->data + sum(root->left) + sum(root->right);
}

// Returns true if the binary tree rooted at 'root' is a Sum Tree.
bool isSumTree(Node *root)
{
    // An empty tree and a leaf node are always Sum Trees.
    if (root == NULL || (root->left == NULL && root->right == NULL))
        return true;

    // Calculate the sum of nodes in the left and right subtrees.
    int leftSum = sum(root->left);
    int rightSum = sum(root->right);

    // Check the Sum Tree property for the current node and
    // recursively verify both subtrees.
    return (root->data == leftSum + rightSum) && isSumTree(root->left) && isSumTree(root->right);
}

int main()
{
    // Create the following binary tree:
    //
    //       3
    //      / \
    //     1   2
    //
    Node *root = createNode(3);
    root->left = createNode(1);
    root->right = createNode(2);

    printf("%s", isSumTree(root) ? "true" : "false");

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

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

class GFG {

    // Returns the sum of all nodes in the subtree rooted at
    // 'root'.
    static int sum(Node root)
    {
        if (root == null)
            return 0;

        return root.data + sum(root.left) + sum(root.right);
    }

    // Returns true if the binary tree rooted at 'root' is a
    // Sum Tree.
    static boolean isSumTree(Node root)
    {
        // An empty tree and a leaf node are always Sum
        // Trees.
        if (root == null
            || (root.left == null && root.right == null))
            return true;

        // Calculate the sum of nodes in the left and right
        // subtrees.
        int leftSum = sum(root.left);
        int rightSum = sum(root.right);

        // Check the Sum Tree property for the current node
        // and recursively verify both subtrees.
        return (root.data == leftSum + rightSum)
            && isSumTree(root.left)
            && isSumTree(root.right);
    }

    public static void main(String[] args)
    {
        // Create the following binary tree:
        //
        //       3
        //      / \
        //     1   2
        //
        Node root = new Node(3);
        root.left = new Node(1);
        root.right = new Node(2);

        System.out.println(isSumTree(root));
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None


# Returns the sum of all nodes in the subtree rooted at 'root'.
def sumTree(root):
    if root is None:
        return 0

    return root.data + sumTree(root.left) + sumTree(root.right)


# Returns True if the binary tree rooted at 'root' is a Sum Tree.
def isSumTree(root):

    # An empty tree and a leaf node are always Sum Trees.
    if root is None or (root.left is None and root.right is None):
        return True

    # Calculate the sum of nodes in the left and right subtrees.
    left_sum = sumTree(root.left)
    right_sum = sumTree(root.right)

    # Check the Sum Tree property for the current node and
    # recursively verify both subtrees.
    return (root.data == left_sum + right_sum and
            isSumTree(root.left) and
            isSumTree(root.right))

# Driver Code

if __name__ == "__main__":
    # Create the following binary tree:
    #
    #       3
    #      / \
    #     1   2
    #
    root = Node(3)
    root.left = Node(1)
    root.right = Node(2)

    print("true" if isSumTree(root) else "false")
C#
using System;

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

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

class GFG {
    // Returns the sum of all nodes in the subtree rooted at
    // 'root'.
    static int Sum(Node root)
    {
        if (root == null)
            return 0;

        return root.data + Sum(root.left) + Sum(root.right);
    }

    // Returns true if the binary tree rooted at 'root' is a
    // Sum Tree.
    static bool isSumTree(Node root)
    {
        // An empty tree and a leaf node are always Sum
        // Trees.
        if (root == null
            || (root.left == null && root.right == null))
            return true;

        // Calculate the sum of nodes in the left and right
        // subtrees.
        int leftSum = Sum(root.left);
        int rightSum = Sum(root.right);

        // Check the Sum Tree property for the current node
        // and recursively verify both subtrees.
        return (root.data == leftSum + rightSum)
            && isSumTree(root.left)
            && isSumTree(root.right);
    }

    static void Main()
    {
        // Create the following binary tree:
        //
        //       3
        //      / \
        //     1   2
        //
        Node root = new Node(3);
        root.left = new Node(1);
        root.right = new Node(2);

        Console.WriteLine(isSumTree(root) ? "true"
                                          : "false");
    }
}
JavaScript
class Node {
    constructor(data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Returns the sum of all nodes in the subtree rooted at
// 'root'.
function sum(root)
{
    if (root === null)
        return 0;

    return root.data + sum(root.left) + sum(root.right);
}

// Returns true if the binary tree rooted at 'root' is a Sum
// Tree.
function isSumTree(root)
{
    // An empty tree and a leaf node are always Sum Trees.
    if (root === null
        || (root.left === null && root.right === null))
        return true;

    // Calculate the sum of nodes in the left and right
    // subtrees.
    const leftSum = sum(root.left);
    const rightSum = sum(root.right);

    // Check the Sum Tree property for the current node and
    // recursively verify both subtrees.
    return (root.data === leftSum + rightSum)
           && isSumTree(root.left) && isSumTree(root.right);
}

// Driver Code
// Create the following binary tree:
//
//       3
//      / \
//     1   2
//
const root = new Node(3);
root.left = new Node(1);
root.right = new Node(2);
console.log(isSumTree(root) ? "true" : "false");

Output
true

[Better Approach] Using Post Order traversal - O(n) Time and O(h) Space

Instead of recomputing the sum of every subtree multiple times, process the tree in postorder so that the left and right subtree sums are available before checking the current node. Each recursive call returns the subtree sum if it is a Sum Tree; otherwise, it immediately propagates failure, allowing the entire tree to be verified in a single traversal.

  • Traverse the tree in postorder (left subtree, right subtree, then current node).
  • If the current node is NULL, return 0; if it is a leaf node, return its value as the subtree sum.
  • Recursively compute the sums of the left and right subtrees.
  • If either recursive call returns -1, immediately return -1 to indicate that the subtree is not a Sum Tree.
  • If the current node's value equals the sum of the left and right subtree sums, return the total subtree sum.
  • Otherwise, return -1 to indicate that the current subtree is not a Sum Tree.
C++
#include <iostream>
using namespace std;

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

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

// Returns the sum of the subtree if it is a Sum Tree.
// Otherwise, returns -1.
int check(Node *root)
{
    // An empty tree has sum 0.
    if (root == nullptr)
        return 0;

    // A leaf node is always a Sum Tree.
    // Return its value as the subtree sum.
    if (root->left == nullptr && root->right == nullptr)
        return root->data;

    // Calculate the sum of the left subtree.
    int leftSum = check(root->left);

    // If the left subtree is not a Sum Tree,
    // propagate the failure.
    if (leftSum == -1)
        return -1;

    // Calculate the sum of the right subtree.
    int rightSum = check(root->right);

    // If the right subtree is not a Sum Tree,
    // propagate the failure.
    if (rightSum == -1)
        return -1;

    // Check whether the current node satisfies
    // the Sum Tree property.
    if (root->data != leftSum + rightSum)
        return -1;

    // Return the total sum of the current subtree.
    return leftSum + rightSum + root->data;
}

// Returns true if the binary tree is a Sum Tree.
bool isSumTree(Node *root)
{
    return check(root) != -1;
}

int main()
{
    // Create the following binary tree:
    //
    //       3
    //      / \
    //     1   2
    //
    Node *root = new Node(3);
    root->left = new Node(1);
    root->right = new Node(2);
    cout << (isSumTree(root) ? "true" : "false");

    return 0;
}
C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
} Node;

// Creates a new tree node.
Node *createNode(int data)
{
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}

// Returns the sum of the subtree if it is a Sum Tree.
// Otherwise, returns -1.
int check(Node *root)
{
    // An empty tree has sum 0.
    if (root == NULL)
        return 0;

    // A leaf node is always a Sum Tree.
    // Return its value as the subtree sum.
    if (root->left == NULL && root->right == NULL)
        return root->data;

    // Calculate the sum of the left subtree.
    int leftSum = check(root->left);

    // If the left subtree is not a Sum Tree,
    // propagate the failure.
    if (leftSum == -1)
        return -1;

    // Calculate the sum of the right subtree.
    int rightSum = check(root->right);

    // If the right subtree is not a Sum Tree,
    // propagate the failure.
    if (rightSum == -1)
        return -1;

    // Check whether the current node satisfies
    // the Sum Tree property.
    if (root->data != leftSum + rightSum)
        return -1;

    // Return the total sum of the current subtree.
    return leftSum + rightSum + root->data;
}

// Returns true if the binary tree is a Sum Tree.
bool isSumTree(Node *root)
{
    return check(root) != -1;
}

int main()
{
    // Create the following binary tree:
    //
    //       3
    //      / \
    //     1   2
    //
    Node *root = createNode(3);
    root->left = createNode(1);
    root->right = createNode(2);

    printf("%s", isSumTree(root) ? "true" : "false");

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

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

class GFG {

    // Returns the sum of the subtree if it is a Sum Tree.
    // Otherwise, returns -1.
    static int check(Node root)
    {
        // An empty tree has sum 0.
        if (root == null)
            return 0;

        // A leaf node is always a Sum Tree.
        // Return its value as the subtree sum.
        if (root.left == null && root.right == null)
            return root.data;

        // Calculate the sum of the left subtree.
        int leftSum = check(root.left);

        // If the left subtree is not a Sum Tree,
        // propagate the failure.
        if (leftSum == -1)
            return -1;

        // Calculate the sum of the right subtree.
        int rightSum = check(root.right);

        // If the right subtree is not a Sum Tree,
        // propagate the failure.
        if (rightSum == -1)
            return -1;

        // Check whether the current node satisfies
        // the Sum Tree property.
        if (root.data != leftSum + rightSum)
            return -1;

        // Return the total sum of the current subtree.
        return leftSum + rightSum + root.data;
    }

    // Returns true if the binary tree is a Sum Tree.
    static boolean isSumTree(Node root)
    {
        return check(root) != -1;
    }

    public static void main(String[] args)
    {
        // Create the following binary tree:
        //
        //       3
        //      / \
        //     1   2
        //
        Node root = new Node(3);
        root.left = new Node(1);
        root.right = new Node(2);

        System.out.println(isSumTree(root));
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None


# Returns the sum of the subtree if it is a Sum Tree.
# Otherwise, returns -1.
def check(root):

    # An empty tree has sum 0.
    if root is None:
        return 0

    # A leaf node is always a Sum Tree.
    # Return its value as the subtree sum.
    if root.left is None and root.right is None:
        return root.data

    # Calculate the sum of the left subtree.
    left_sum = check(root.left)

    # If the left subtree is not a Sum Tree,
    # propagate the failure.
    if left_sum == -1:
        return -1

    # Calculate the sum of the right subtree.
    right_sum = check(root.right)

    # If the right subtree is not a Sum Tree,
    # propagate the failure.
    if right_sum == -1:
        return -1

    # Check whether the current node satisfies
    # the Sum Tree property.
    if root.data != left_sum + right_sum:
        return -1

    # Return the total sum of the current subtree.
    return left_sum + right_sum + root.data


# Returns True if the binary tree is a Sum Tree.
def isSumTree(root):
    return check(root) != -1


# Driver Code

if __name__ == "__main__":
    # Create the following binary tree:
    #
    #       3
    #      / \
    #     1   2
    #
    root = Node(3)
    root.left = Node(1)
    root.right = Node(2)

    print("true" if isSumTree(root) else "false")
C#
using System;

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

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

class GFG {
    
    // Returns the sum of the subtree if it is a Sum Tree.
    // Otherwise, returns -1.
    static int Check(Node root)
    {
        // An empty tree has sum 0.
        if (root == null)
            return 0;

        // A leaf node is always a Sum Tree.
        // Return its value as the subtree sum.
        if (root.left == null && root.right == null)
            return root.data;

        // Calculate the sum of the left subtree.
        int leftSum = Check(root.left);

        // If the left subtree is not a Sum Tree,
        // propagate the failure.
        if (leftSum == -1)
            return -1;

        // Calculate the sum of the right subtree.
        int rightSum = Check(root.right);

        // If the right subtree is not a Sum Tree,
        // propagate the failure.
        if (rightSum == -1)
            return -1;

        // Check whether the current node satisfies
        // the Sum Tree property.
        if (root.data != leftSum + rightSum)
            return -1;

        // Return the total sum of the current subtree.
        return leftSum + rightSum + root.data;
    }

    // Returns true if the binary tree is a Sum Tree.
    static bool isSumTree(Node root)
    {
        return Check(root) != -1;
    }

    static void Main()
    {
        // Create the following binary tree:
        //
        //       3
        //      / \
        //     1   2
        //
        Node root = new Node(3);
        root.left = new Node(1);
        root.right = new Node(2);

        Console.WriteLine(isSumTree(root) ? "true"
                                          : "false");
    }
}
JavaScript
class Node {
    constructor(data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Returns the sum of the subtree if it is a Sum Tree.
// Otherwise, returns -1.
function check(root)
{
    // An empty tree has sum 0.
    if (root === null)
        return 0;

    // A leaf node is always a Sum Tree.
    // Return its value as the subtree sum.
    if (root.left === null && root.right === null)
        return root.data;

    // Calculate the sum of the left subtree.
    const leftSum = check(root.left);

    // If the left subtree is not a Sum Tree,
    // propagate the failure.
    if (leftSum === -1)
        return -1;

    // Calculate the sum of the right subtree.
    const rightSum = check(root.right);

    // If the right subtree is not a Sum Tree,
    // propagate the failure.
    if (rightSum === -1)
        return -1;

    // Check whether the current node satisfies
    // the Sum Tree property.
    if (root.data !== leftSum + rightSum)
        return -1;

    // Return the total sum of the current subtree.
    return leftSum + rightSum + root.data;
}

// Returns true if the binary tree is a Sum Tree.
function isSumTree(root) { 
    return check(root) !== -1; 
}


// Driver Code

// Create the following binary tree:
//
//       3
//      / \
//     1   2
//
const root = new Node(3);
root.left = new Node(1);
root.right = new Node(2);

console.log(isSumTree(root) ? "true" : "false");

Output
true

[Expected Approach] Using Sum Tree Property - O(n) Time and O(h) Space

A key property of a Sum Tree is that for every non-leaf Sum Tree node, the sum of all nodes in its subtree is equal to 2 × node->data. By first verifying that the left and right subtrees are Sum Trees, we can compute their sums in O(1) using this property instead of traversing the subtrees again.

  • If the current node is NULL or a leaf node, return true.
  • Recursively check whether the left and right subtrees are Sum Trees.
  • If either subtree is not a Sum Tree, return false.
  • Compute the left and right subtree sums in O(1) using the Sum Tree property (0 for NULL, node value for a leaf, otherwise 2 × child->data).
  • Check whether the current node's value equals the sum of the left and right subtree sums.
  • Return true if the condition holds; otherwise, return false.
C++
#include <iostream>
using namespace std;

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

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

// Returns true if the given node is a leaf node.
bool isLeaf(Node *node)
{
    return node != nullptr && node->left == nullptr && node->right == nullptr;
}

// Returns true if the binary tree rooted at 'root' is a Sum Tree.
bool isSumTree(Node *root)
{
    int leftSum, rightSum;

    // An empty tree and a leaf node are always Sum Trees.
    if (root == nullptr || isLeaf(root))
        return true;

    // First, verify that both left and right subtrees are Sum Trees.
    if (isSumTree(root->left) && isSumTree(root->right))
    {
        // Calculate the sum of the left subtree in O(1).
        if (root->left == nullptr)
            leftSum = 0;
        else if (isLeaf(root->left))
            leftSum = root->left->data;
        else
            leftSum = 2 * root->left->data;

        // Calculate the sum of the right subtree in O(1).
        if (root->right == nullptr)
            rightSum = 0;
        else if (isLeaf(root->right))
            rightSum = root->right->data;
        else
            rightSum = 2 * root->right->data;

        // Check whether the current node satisfies
        // the Sum Tree property.
        return root->data == leftSum + rightSum;
    }

    // If either subtree is not a Sum Tree,
    // then the current tree cannot be a Sum Tree.
    return false;
}

int main()
{
    // Create the following binary tree:
    //
    //       3
    //      / \
    //     1   2
    //
    Node *root = new Node(3);
    root->left = new Node(1);
    root->right = new Node(2);

    cout << (isSumTree(root) ? "true" : "false");

    return 0;
}
C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
} Node;

// Creates a new tree node.
Node *createNode(int data)
{
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}

// Returns true if the given node is a leaf node.
bool isLeaf(Node *node)
{
    return node != NULL && node->left == NULL && node->right == NULL;
}

// Returns true if the binary tree rooted at 'root' is a Sum Tree.
bool isSumTree(Node *root)
{
    int leftSum, rightSum;

    // An empty tree and a leaf node are always Sum Trees.
    if (root == NULL || isLeaf(root))
        return true;

    // First, verify that both left and right subtrees are Sum Trees.
    if (isSumTree(root->left) && isSumTree(root->right))
    {
        // Calculate the sum of the left subtree in O(1).
        if (root->left == NULL)
            leftSum = 0;
        else if (isLeaf(root->left))
            leftSum = root->left->data;
        else
            leftSum = 2 * root->left->data;

        // Calculate the sum of the right subtree in O(1).
        if (root->right == NULL)
            rightSum = 0;
        else if (isLeaf(root->right))
            rightSum = root->right->data;
        else
            rightSum = 2 * root->right->data;

        // Check whether the current node satisfies
        // the Sum Tree property.
        return root->data == leftSum + rightSum;
    }

    // If either subtree is not a Sum Tree,
    // then the current tree cannot be a Sum Tree.
    return false;
}

int main()
{
    // Create the following binary tree:
    //
    //       3
    //      / \
    //     1   2
    //
    Node *root = createNode(3);
    root->left = createNode(1);
    root->right = createNode(2);

    printf("%s", isSumTree(root) ? "true" : "false");

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

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

class GFG {

    // Returns true if the given node is a leaf node.
    static boolean isLeaf(Node node)
    {
        return node != null && node.left == null
            && node.right == null;
    }

    // Returns true if the binary tree rooted at 'root' is a
    // Sum Tree.
    static boolean isSumTree(Node root)
    {
        int leftSum, rightSum;

        // An empty tree and a leaf node are always Sum
        // Trees.
        if (root == null || isLeaf(root))
            return true;

        // First, verify that both left and right subtrees
        // are Sum Trees.
        if (isSumTree(root.left) && isSumTree(root.right)) {

            // Calculate the sum of the left subtree in
            // O(1).
            if (root.left == null)
                leftSum = 0;
            else if (isLeaf(root.left))
                leftSum = root.left.data;
            else
                leftSum = 2 * root.left.data;

            // Calculate the sum of the right subtree in
            // O(1).
            if (root.right == null)
                rightSum = 0;
            else if (isLeaf(root.right))
                rightSum = root.right.data;
            else
                rightSum = 2 * root.right.data;

            // Check whether the current node satisfies
            // the Sum Tree property.
            return root.data == leftSum + rightSum;
        }

        // If either subtree is not a Sum Tree,
        // then the current tree cannot be a Sum Tree.
        return false;
    }

    public static void main(String[] args)
    {
        // Create the following binary tree:
        //
        //       3
        //      / \
        //     1   2
        //
        Node root = new Node(3);
        root.left = new Node(1);
        root.right = new Node(2);

        System.out.println(isSumTree(root));
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None


# Returns True if the given node is a leaf node.
def is_leaf(node):
    return node is not None and node.left is None and node.right is None


# Returns True if the binary tree rooted at 'root' is a Sum Tree.
def isSumTree(root):

    # An empty tree and a leaf node are always Sum Trees.
    if root is None or is_leaf(root):
        return True

    # First, verify that both left and right subtrees are Sum Trees.
    if isSumTree(root.left) and isSumTree(root.right):

        # Calculate the sum of the left subtree in O(1).
        if root.left is None:
            left_sum = 0
        elif is_leaf(root.left):
            left_sum = root.left.data
        else:
            left_sum = 2 * root.left.data

        # Calculate the sum of the right subtree in O(1).
        if root.right is None:
            right_sum = 0
        elif is_leaf(root.right):
            right_sum = root.right.data
        else:
            right_sum = 2 * root.right.data

        # Check whether the current node satisfies
        # the Sum Tree property.
        return root.data == left_sum + right_sum

    # If either subtree is not a Sum Tree,
    # then the current tree cannot be a Sum Tree.
    return False

if __name__ == "__main__":
    # Create the following binary tree:
    #
    #       3
    #      / \
    #     1   2
    #
    root = Node(3)
    root.left = Node(1)
    root.right = Node(2)

    print("true" if isSumTree(root) else "false")
C#
using System;

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

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

class GFG {
    
    // Returns true if the given node is a leaf node.
    static bool IsLeaf(Node node)
    {
        return node != null && node.left == null
            && node.right == null;
    }

    // Returns true if the binary tree rooted at 'root' is a
    // Sum Tree.
    static bool isSumTree(Node root)
    {
        int leftSum, rightSum;

        // An empty tree and a leaf node are always Sum
        // Trees.
        if (root == null || IsLeaf(root))
            return true;

        // First, verify that both left and right subtrees
        // are Sum Trees.
        if (isSumTree(root.left) && isSumTree(root.right)) {
            // Calculate the sum of the left subtree in
            // O(1).
            if (root.left == null)
                leftSum = 0;
            else if (IsLeaf(root.left))
                leftSum = root.left.data;
            else
                leftSum = 2 * root.left.data;

            // Calculate the sum of the right subtree in
            // O(1).
            if (root.right == null)
                rightSum = 0;
            else if (IsLeaf(root.right))
                rightSum = root.right.data;
            else
                rightSum = 2 * root.right.data;

            // Check whether the current node satisfies
            // the Sum Tree property.
            return root.data == leftSum + rightSum;
        }

        // If either subtree is not a Sum Tree,
        // then the current tree cannot be a Sum Tree.
        return false;
    }

    static void Main()
    {
        // Create the following binary tree:
        //
        //       3
        //      / \
        //     1   2
        //
        Node root = new Node(3);
        root.left = new Node(1);
        root.right = new Node(2);

        Console.WriteLine(isSumTree(root) ? "true"
                                          : "false");
    }
}
JavaScript
class Node {
    constructor(data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Returns true if the given node is a leaf node.
function isLeaf(node)
{
    return node !== null && node.left === null
           && node.right === null;
}

// Returns true if the binary tree rooted at 'root' is a Sum
// Tree.
function isSumTree(root)
{
    let leftSum, rightSum;

    // An empty tree and a leaf node are always Sum Trees.
    if (root === null || isLeaf(root))
        return true;

    // First, verify that both left and right subtrees are
    // Sum Trees.
    if (isSumTree(root.left) && isSumTree(root.right)) {

        // Calculate the sum of the left subtree in O(1).
        if (root.left === null)
            leftSum = 0;
        else if (isLeaf(root.left))
            leftSum = root.left.data;
        else
            leftSum = 2 * root.left.data;

        // Calculate the sum of the right subtree in O(1).
        if (root.right === null)
            rightSum = 0;
        else if (isLeaf(root.right))
            rightSum = root.right.data;
        else
            rightSum = 2 * root.right.data;

        // Check whether the current node satisfies
        // the Sum Tree property.
        return root.data === leftSum + rightSum;
    }

    // If either subtree is not a Sum Tree,
    // then the current tree cannot be a Sum Tree.
    return false;
}

// Driver Code
// Create the following binary tree:
//
//       3
//      / \
//     1   2
//
const root = new Node(3);
root.left = new Node(1);
root.right = new Node(2);

console.log(isSumTree(root) ? "true" : "false");

Output
true
Comment