Sum Tree

Sum Tree

Given a Binary Tree. Check for the Sum Tree for every node except the leaf node. Return true if it is a Sum Tree otherwise, return false.

A SumTree is a Binary Tree where the value of a node is equal to the sum of the nodes present in its left subtree and right subtree. An empty tree is also a Sum Tree as the sum of an empty tree can be considered to be 0. A leaf node is also considered a Sum Tree.

?? View Full Code ??


Approach:

The most efficient way to solve this problem is to use a recursive post-order traversal. This approach allows us to calculate subtree sums from the bottom up, making it easier to check the sum property for each non-leaf node. Here’s a step-by-step breakdown of the approach:

  1. Base Case: If the current node is None, we return 0 as the sum.If the node is a leaf, we return its value as it satisfies the Sum Tree condition by definition.
  2. Recursive Post-Order Traversal: Recursively calculate the sum of the left and right subtrees. For each non-leaf node, check if its value is equal to the sum of the values of its left and right subtrees.
  3. Return Sum and Validity: If a node violates the Sum Tree property, we return an invalid signal to stop further calculations. If all nodes meet the Sum Tree condition, we continue up the tree, accumulating sums to return the final result.

pair<bool,int>solve(Node* root){
        
        if(root == NULL){
            pair<bool,int>p = make_pair(true, 0);
            return p;
        }
        if(root->left == NULL && root->right == NULL){
            pair<bool,int>p = make_pair(true, root->data);
            return p;
        }
        
        pair<bool,int>left = solve(root->left);
        pair<bool,int>right = solve(root->right);
        
        bool isSumTreeNot = (root->data == left.second + right.second);
        
        bool check = left.first && right.first && isSumTreeNot;
        int sum = root->data + left.second + right.second;
        
        return make_pair(check, sum);
    }
    
    bool isSumTree(Node* root)
    {
        
        return solve(root).first;
    }        

Explanation :

  • Base Conditions: We start by checking if the node is None or a leaf. If None, we return 0; if it’s a leaf, we return its value.
  • Recursive Calls: We recursively calculate the sum of the left and right subtrees.
  • Condition Check: For each non-leaf node, we verify if the node’s value equals the sum of its left and right subtree sums.
  • Return Values: If a node violates the Sum Tree property, we return -1 to stop further checks.


Time Complexity : O(N)

Space Complexity : O(N)


?? View GitHub ??



要查看或添加评论,请登录

Pranay Saini的更多文章

  • Maximum sum of nodes in Binary tree such that no two are adjacent

    Maximum sum of nodes in Binary tree such that no two are adjacent

    Given a binary tree with a value associated with each node, we need to choose a subset of these nodes such that sum of…

    2 条评论
  • Largest Subtree Sum

    Largest Subtree Sum

    Given a binary tree. The task is to find subtree with maximum sum in the tree and return its sum.

    2 条评论
  • Sum of Longest Blood Line of Tree

    Sum of Longest Blood Line of Tree

    Given a binary tree having n nodes. Find the sum of all nodes on the longest path from root to any leaf node.

  • Check Mirror Tree

    Check Mirror Tree

    Given two n-ary trees. Check if they are mirror images of each other or not.

    2 条评论
  • Duplicate Subtree

    Duplicate Subtree

    Given a binary tree, find out whether it contains a duplicate sub-tree of size two or more, or not. Note: Two same leaf…

    2 条评论
  • Leaves at Same Level

    Leaves at Same Level

    Given a binary tree with n nodes, determine whether all the leaf nodes are at the same level or not. Return true if all…

  • Transform to Sum Tree

    Transform to Sum Tree

    Given a Binary Tree of size N , where each node can have positive or negative values. Convert this to a tree where each…

    2 条评论
  • Boundary Traversal of Binary Tree

    Boundary Traversal of Binary Tree

    Given a Binary Tree, find its Boundary Traversal. The traversal should be in the following order: Left boundary nodes:…

  • Diagonal Traversal of Binary Tree

    Diagonal Traversal of Binary Tree

    Consider lines of slope -1 passing between nodes. Given a Binary Tree, print all diagonal elements in a binary tree…

    2 条评论
  • Balanced Tree Check

    Balanced Tree Check

    A binary tree is height-balanced if, for every node in the tree, the difference in heights of the left and right…

    2 条评论

社区洞察

其他会员也浏览了