Convert a Binary Tree into a Sum Tree

Problem Statement

Given a binary tree, convert it into a Sum Tree. In a Sum Tree, each node is assigned a new value which is the sum of the values of its left and right subtrees in the original tree. The value of a leaf node becomes 0 because it has no children. If the tree is empty, return null or handle it as a base case.

Examples

Input Tree Level Order Output Description
[10, -2, 6, 8, -4, null, 5]
[[13], [4, 5], [0, 0, 0]] Standard tree: Converted to sum tree where each node is the sum of values in its left and right subtrees
[1]
[[0]] Edge case: A single node becomes 0 as it has no children
[] [] Edge case: Empty tree
[5, 3, null, 2, null, null, null, 1]
[[6], [3], [1], [0]] Deep left-skewed tree converted into a sum tree; each node is updated recursively
[7, null, 9, null, null, null, 11]
[[20], [11], [0]] Right-skewed tree: Each node updated to sum of subtree

Solution

Understanding the Problem

The task is to convert a given binary tree into a sum tree. In a sum tree, each node’s value is replaced by the sum of values of its left and right subtrees in the original tree. For leaf nodes, since they don’t have children, their value becomes 0.

This transformation is done recursively, which means for each node, we first compute the sum of its left and right subtrees and update the node’s value, then repeat the process for its children.

Step-by-Step Solution with Example

Step 1: Understand Tree Structure

Consider this example binary tree:


        10
       /        -2    6
     /    /     8  -4 7   5

We need to convert this into a sum tree. For every node, we’ll replace its value with the sum of values in its left and right subtrees from the original tree.

Step 2: Process Leaf Nodes

Leaf nodes have no children, so their new value becomes 0.

  • 8 → 0
  • -4 → 0
  • 7 → 0
  • 5 → 0

Step 3: Process Intermediate Nodes

Now, we go one level up and calculate the new values based on the original values of children.

  • -2 has children 8 and -4 → new value = 8 + (-4) = 4
  • 6 has children 7 and 5 → new value = 7 + 5 = 12

Step 4: Process Root Node

Finally, we compute the value for the root node.

  • 10 has children -2 and 6 → in original tree, their values were -2 and 6 → new value = -2 + 6 = 4

Step 5: Update Tree

After the transformation, the sum tree looks like this:


        4
       /        4    12
     /    /     0   0 0   0

Edge Cases

Case 1: Tree with Multiple Levels

This is the general case like the example above. Each internal node gets updated based on the sum of its children’s original values. Leaf nodes become 0.

Case 2: Tree with a Single Node

If the tree only has one node, there are no children to sum. Therefore, the single node becomes 0, since it's considered a leaf node.

Case 3: Empty Tree

If the input tree is empty (i.e., the root is null), then the output is also an empty tree or null. This is the base case in our recursion.

Finally

Converting a binary tree to a sum tree requires a post-order traversal, where we visit left and right children before updating the current node. The solution handles all edge cases and maintains correctness by updating nodes only after their subtrees are processed. Always remember to handle base cases like leaf nodes and empty trees to avoid runtime errors.

Algorithm Steps

  1. If the tree is empty, return 0.
  2. Recursively convert the left subtree into a sum tree and obtain its sum.
  3. Recursively convert the right subtree into a sum tree and obtain its sum.
  4. Store the current node's original value in a temporary variable, e.g., oldVal.
  5. Update the current node's value to the sum of the left and right subtree sums.
  6. Return the sum of the updated node value and the original value (node.val + oldVal) so it can be used by the parent node.

Code

C
C++
Python
Java
JS
Go
Rust
Kotlin
TS
#include <stdio.h>
#include <stdlib.h>

typedef struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;

TreeNode* createNode(int val) {
    TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
    node->val = val;
    node->left = node->right = NULL;
    return node;
}

int toSumTree(TreeNode* root) {
    if (root == NULL) return 0;
    int oldVal = root->val;
    int leftSum = toSumTree(root->left);
    int rightSum = toSumTree(root->right);
    root->val = leftSum + rightSum;
    return root->val + oldVal;
}

int main() {
    TreeNode* root = createNode(10);
    root->left = createNode(-2);
    root->left->left = createNode(8);
    root->left->right = createNode(-4);
    root->right = createNode(6);
    toSumTree(root);
    printf("Root value after conversion: %d\n", root->val);
    return 0;
}

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...