Open In App

Convert an arbitrary Binary Tree to a tree that holds Children Sum Property

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an arbitrary binary tree, your task is to convert it to a binary tree that holds Children Sum Property, by incrementing the data values of any node.

Note: The structure of the tree can't be changed and the node values can't be decremented. Also, there exist multiple possible answers.

Example:

Input: 50
/ \
7 2
/ \ / \
3 5 1 30
Output: 50
/ \
19 31
/ \ / \
14 5 1 30
Explanation: For every node, now its value is equal to the sum of values of its immediate left and right child. One more possible solution is:

79
/ \
48 31
/ \ / \
43 5 1 30

[Naive Approach] - O(n ^ 2) Time and O(h) Space

The idea is to traverse the given tree in post order to firstly convert the left and right children to hold the children sum property and then change the parent node.
After converting the child nodes, find the difference diff between node's data and sum of children nodes i.e. diff = node’s children sum - node’s data.

There are three possibilities:

  • diff == 0: Nothing needs to be done, as the node follows the child sum property:
  • diff > 0: Nodes data is smaller than sum of children node's data, thus increment the node's data by diff.
  • diff < 0: Nodes data is greater than sum of children node's data, then increment one child's data.

Incrementing a child changes the subtree’s children sum property so we need to change left subtree also. So we recursively increment the left child. If left child is empty then we recursively call increment() for right child.

Let us run the algorithm for the given example:

50
/ \
7 2
/ \ / \
3 5 1 30 

First convert the left subtree (increment 7 to 8). 

50
/ \
8 2
/ \ / \
3 5 1 30

Then convert the right subtree (increment 2 to 31)

50
/ \
8 31
/ \ / \
3 5 1 30

Now convert the root, we have to increment left subtree for converting the root. 

50
/ \
19 31
/ \ / \
14 5 1 30

Please note the last step – we have incremented 8 to 19, and to fix the subtree we have incremented 3 to 14.

C++
#include<bits/stdc++.h> 
using namespace std;

// Node structure
class Node { 
public:
    int data; 
    Node* left; 
    Node* right; 
    Node(int data) {
        this->data = data; 
        this->left = nullptr; 
        this->right = nullptr;
    }
}; 

// Function to increment the left subtree
void increment(Node* cur, int diff)  { 

    // If left child is not 
    // nullptr then increment it
    if(cur->left != nullptr) { 
        cur->left->data = cur->left->data + diff; 
    
        // Recursively call to fix 
        // the descendants of Node->left 
        increment(cur->left, diff); 
    } 

    // Else increment right child 
    else if (cur->right != nullptr) { 
        cur->right->data = cur->right->data + diff; 
    
        // Recursively call to fix 
        // the descendants of Node->right 
        increment(cur->right, diff); 
    } 
} 

// Function to modify a tree 
// to hold children sum property
void convertTree(Node* cur) {

    // If tree is empty or it's a 
    // leaf Node then return true
    if (cur == nullptr || (cur->left == nullptr && 
        cur->right == nullptr)) 
        return; 
        
        // convert left and right subtrees first
        convertTree(cur->left); 
        convertTree(cur->right); 
        
    int leftData = 0, rightData = 0;

    // update left and right child data
    if (cur->left != nullptr) 
        leftData = cur->left->data; 

    if (cur->right != nullptr) 
        rightData = cur->right->data; 

    // get the diff of Node's data and children sum
    int diff = leftData + rightData - cur->data; 

    // if Node's children sum is 
    // greater than the Node's data
    if (diff > 0) 
        cur->data = cur->data + diff; 

    // else if Node's data is greater than
    // children sum then increment subtree
    else if (diff < 0) 
        increment(cur, abs(diff));
} 


// prints the inorder traversal of tree
void printInorder(Node* cur)  { 
    if (cur == nullptr) 
        return; 
    
    printInorder(cur->left); 
    cout<<cur->data<<" ";
    printInorder(cur->right); 
} 

int main()  { 
    // construct the binary tree
    /*
        50
       /  \     
      7    2
     / \  / \
    3   5 1  30    */
    
    Node *root = new Node(50); 
    root->left = new Node(7); 
    root->right = new Node(2); 
    root->left->left = new Node(3); 
    root->left->right = new Node(5); 
    root->right->left = new Node(1); 
    root->right->right = new Node(30); 
    printInorder(root);
    cout<<endl;

    convertTree(root); 
    
    printInorder(root); 
    return 0; 
}
Java
// Node structure
class Node {
    int data;
    Node left;
    Node right;
    
    Node(int data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Function to increment the left subtree
class GfG {

    // Function to increment the left subtree
    static void increment(Node cur, int diff) {
        // If left child is not 
        // null then increment it
        if (cur.left != null) {
            cur.left.data = cur.left.data + diff;
            
            // Recursively call to fix 
            // the descendants of Node->left 
            increment(cur.left, diff);
        }
        // Else increment right child 
        else if (cur.right != null) {
            cur.right.data = cur.right.data + diff;
            
            // Recursively call to fix 
            // the descendants of Node->right 
            increment(cur.right, diff);
        }
    }
    
    // Function to modify a tree 
    // to hold children sum property
    static void convertTree(Node cur) {
        // If tree is empty or it's a 
        // leaf Node then return true
        if (cur == null || (cur.left == null && cur.right == null))
            return;
        
        // convert left and right subtrees first
        convertTree(cur.left);
        convertTree(cur.right);
        
        int leftData = 0, rightData = 0;
        
        // update left and right child data
        if (cur.left != null)
            leftData = cur.left.data;
        
        if (cur.right != null)
            rightData = cur.right.data;
        
        // get the diff of Node's data and children sum
        int diff = leftData + rightData - cur.data;
        
        // if Node's children sum is 
        // greater than the Node's data
        if (diff > 0)
            cur.data = cur.data + diff;
        // else if Node's data is greater than
        // children sum then increment subtree
        else if (diff < 0)
            increment(cur, Math.abs(diff));
    }
    
    // prints the inorder traversal of tree
    static void printInorder(Node cur) {
        if (cur == null)
            return;
        
        printInorder(cur.left);
        System.out.print(cur.data + " ");
        printInorder(cur.right);
    }
    
    public static void main(String[] args) {
        // construct the binary tree
        /*
            50
           /  \     
          7    2
         / \  / \
        3   5 1  30    */
        Node root = new Node(50);
        root.left = new Node(7);
        root.right = new Node(2);
        root.left.left = new Node(3);
        root.left.right = new Node(5);
        root.right.left = new Node(1);
        root.right.right = new Node(30);
        printInorder(root);
        System.out.println();
 
        convertTree(root);
 
        printInorder(root);
    }
}
Python
# Node structure
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

# Function to increment the left subtree
def increment(cur, diff):
    # If left child is not 
    # None then increment it
    if cur.left is not None:
        cur.left.data = cur.left.data + diff
        
        # Recursively call to fix 
        # the descendants of Node->left 
        increment(cur.left, diff)
    # Else increment right child 
    elif cur.right is not None:
        cur.right.data = cur.right.data + diff
        
        # Recursively call to fix 
        # the descendants of Node->right 
        increment(cur.right, diff)

# Function to modify a tree 
# to hold children sum property
def convertTree(cur):
    # If tree is empty or it's a 
    # leaf Node then return true
    if cur is None or (cur.left is None and cur.right is None):
        return
    
    # convert left and right subtrees first
    convertTree(cur.left)
    convertTree(cur.right)
    
    leftData = 0
    rightData = 0
    
    # update left and right child data
    if cur.left is not None:
        leftData = cur.left.data
    
    if cur.right is not None:
        rightData = cur.right.data
    
    # get the diff of Node's data and children sum
    diff = leftData + rightData - cur.data
    
    # if Node's children sum is 
    # greater than the Node's data
    if diff > 0:
        cur.data = cur.data + diff
    # else if Node's data is greater than
    # children sum then increment subtree
    elif diff < 0:
        increment(cur, abs(diff))

# prints the inorder traversal of tree
def printInorder(cur):
    if cur is None:
        return
    printInorder(cur.left)
    print(cur.data, end=" ")
    printInorder(cur.right)

if __name__ == "__main__":
    # construct the binary tree
    
    #    50
    #   /  \     
    #  7    2
    # / \  / \
    #3   5 1  30    
    root = Node(50)
    root.left = Node(7)
    root.right = Node(2)
    root.left.left = Node(3)
    root.left.right = Node(5)
    root.right.left = Node(1)
    root.right.right = Node(30)
    printInorder(root)
    print()
 
    convertTree(root)
 
    printInorder(root)
C#
// Node structure
using System;
using System.Collections.Generic;

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

// Function to increment the left subtree
class GfG {
    
    // Function to increment the left subtree
    static void increment(Node cur, int diff) {
        // If left child is not 
        // null then increment it
        if (cur.left != null) {
            cur.left.data = cur.left.data + diff;
            
            // Recursively call to fix 
            // the descendants of Node->left 
            increment(cur.left, diff);
        }
        // Else increment right child 
        else if (cur.right != null) {
            cur.right.data = cur.right.data + diff;
            
            // Recursively call to fix 
            // the descendants of Node->right 
            increment(cur.right, diff);
        }
    }
    
    // Function to modify a tree 
    // to hold children sum property
    static void convertTree(Node cur) {
        // If tree is empty or it's a 
        // leaf Node then return true
        if (cur == null || (cur.left == null && cur.right == null))
            return;
        
        // convert left and right subtrees first
        convertTree(cur.left);
        convertTree(cur.right);
        
        int leftData = 0, rightData = 0;
        
        // update left and right child data
        if (cur.left != null)
            leftData = cur.left.data;
        
        if (cur.right != null)
            rightData = cur.right.data;
        
        // get the diff of Node's data and children sum
        int diff = leftData + rightData - cur.data;
        
        // if Node's children sum is 
        // greater than the Node's data
        if (diff > 0)
            cur.data = cur.data + diff;
        // else if Node's data is greater than
        // children sum then increment subtree
        else if (diff < 0)
            increment(cur, Math.Abs(diff));
    }
    
    // prints the inorder traversal of tree
    static void printInorder(Node cur) {
        if (cur == null)
            return;
        printInorder(cur.left);
        Console.Write(cur.data + " ");
        printInorder(cur.right);
    }
    
    static void Main() {
        // construct the binary tree
        /*
            50
           /  \     
          7    2
         / \  / \
        3   5 1  30    */
        Node root = new Node(50);
        root.left = new Node(7);
        root.right = new Node(2);
        root.left.left = new Node(3);
        root.left.right = new Node(5);
        root.right.left = new Node(1);
        root.right.right = new Node(30);
        printInorder(root);
        Console.WriteLine();
 
        convertTree(root);
 
        printInorder(root);
    }
}
JavaScript
// Node structure
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Function to increment the left subtree
function increment(cur, diff) {
    
    // If left child is not 
    // null then increment it
    if (cur.left !== null) {
        cur.left.data = cur.left.data + diff;
        
        // Recursively call to fix 
        // the descendants of Node->left 
        increment(cur.left, diff);
    }
    
    // Else increment right child 
    else if (cur.right !== null) {
        cur.right.data = cur.right.data + diff;
        
        // Recursively call to fix 
        // the descendants of Node->right 
        increment(cur.right, diff);
    }
}

// Function to modify a tree 
// to hold children sum property
function convertTree(cur) {
    
    // If tree is empty or it's a 
    // leaf Node then return true
    if (cur === null || (cur.left === null && cur.right === null))
        return;
    
    // convert left and right subtrees first
    convertTree(cur.left);
    convertTree(cur.right);
    
    let leftData = 0, rightData = 0;
    
    // update left and right child data
    if (cur.left !== null)
        leftData = cur.left.data;
    
    if (cur.right !== null)
        rightData = cur.right.data;
    
    // get the diff of Node's data and children sum
    let diff = leftData + rightData - cur.data;
    
    // if Node's children sum is 
    // greater than the Node's data
    if (diff > 0)
        cur.data = cur.data + diff;
    // else if Node's data is greater than
    // children sum then increment subtree
    else if (diff < 0)
        increment(cur, Math.abs(diff));
}

// prints the inorder traversal of tree
function printInorder(cur) {
    if (cur === null)
        return;
    printInorder(cur.left);
    process.stdout.write(cur.data + " ");
    printInorder(cur.right);
}

function main() {
    
    // construct the binary tree
    /*
        50
       /  \     
      7    2
     / \  / \
    3   5 1  30    */
    let root = new Node(50);
    root.left = new Node(7);
    root.right = new Node(2);
    root.left.left = new Node(3);
    root.left.right = new Node(5);
    root.right.left = new Node(1);
    root.right.right = new Node(30);
    printInorder(root);
    console.log("");
    
    convertTree(root);
    
    printInorder(root);
}
 
main();

Output
3 7 5 50 1 2 30 
14 19 5 50 1 31 30 

Time Complexity: O(n ^ 2), worst case complexity is for a skewed tree such that nodes are in decreasing order from root to leaf.
Auxiliary Space : O(h) where h is the height of the binary tree.

[Expected Approach] - O(n) Time and O(h) Space

The idea is to modify the children nodes in top-down fashion while fix the children sum property in bottom-up fashion. To make sure that the parent's node data is not greater than sum of children nodes data, update the child nodes while going down the tree. And while returning sum the child nodes data and update the parent node's data accordingly.

Consider the following example:

50
/ \
7 2
/ \ / \
3 5 1 30 

Here, the issue is having shortage while summing, like 1 + 30 =31 then 3 + 5 = 8 => 31+8 =39, but u cannot decrement 50 to 39 as per rule.

  • So while going down the tree increase the value so to make sure we don't end up with shortage.
  • Then all we need to do is, while returning, just sum the children and replace the parent node.
  • At last, the children sum property holds TRUE. (As there is no restriction on the value needs to be minimum as such).
C++
#include<bits/stdc++.h> 
using namespace std;

// Node structure
class Node { 
public:
    int data; 
    Node* left; 
    Node* right; 
    Node(int data) {
        this->data = data; 
        this->left = nullptr; 
        this->right = nullptr;
    }
}; 

// Function to modify a tree 
// to hold children sum property
void convertTree(Node* root) {
    
    // if root is null, return
    if (!root) return;

    // find the left and right child sum
    int childSum = 0;

    if (root->left)
        childSum += root->left->data;
    if (root->right)
        childSum += root->right->data;
    
    // if the root's data is less than 
    // the sum of its children
    if (root->data < childSum) {
        root->data = childSum;
    } 
    
    // if the root's data is greater than
    // the sum of its children
    else if (root->data > childSum) {
        int diff = root->data - childSum;

        // if left child is not null
        if (root->left) 
            root->left->data += diff;
        
        // else if right child is not null
        else if (root->right)
            root->right->data += diff;
    }
    
    // modify the left and right subtree
    convertTree(root->left);
    convertTree(root->right);

    // update the root's data to sum
    // of child node's data
    childSum = 0;

    if (root->left)
        childSum += root->left->data;
    if (root->right)
        childSum += root->right->data;

    // if root is not leaf node
    if(root->left || root->right) 
        root->data = childSum;
}

// prints the inorder traversal of tree
void printInorder(Node* cur)  { 
    if (cur == nullptr) 
        return; 
    
    printInorder(cur->left); 
    cout<<cur->data<<" ";
    printInorder(cur->right); 
} 

int main()  { 
    // construct the binary tree
    /*
        50
       /  \     
      7    2
     / \  / \
    3   5 1  30    */
    
    Node *root = new Node(50); 
    root->left = new Node(7); 
    root->right = new Node(2); 
    root->left->left = new Node(3); 
    root->left->right = new Node(5); 
    root->right->left = new Node(1); 
    root->right->right = new Node(30); 
    printInorder(root);
    cout<<endl;

    convertTree(root); 
    
    printInorder(root); 
    return 0; 
}
Java
// Node structure
class Node {
    int data;
    Node left;
    Node right;
    
    Node(int data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Function to modify a tree 
// to hold children sum property
class GfG {

    static void convertTree(Node root) {
        
        // if root is null, return
        if (root == null)
            return;
        
        // find the left and right child sum
        int childSum = 0;
        
        if (root.left != null)
            childSum += root.left.data;
        if (root.right != null)
            childSum += root.right.data;
        
        // if the root's data is less than 
        // the sum of its children
        if (root.data < childSum) {
            root.data = childSum;
        } 
        // if the root's data is greater than
        // the sum of its children
        else if (root.data > childSum) {
            int diff = root.data - childSum;
            
            // if left child is not null
            if (root.left != null)
                root.left.data += diff;
            
            // else if right child is not null
            else if (root.right != null)
                root.right.data += diff;
        }
        
        // modify the left and right subtree
        convertTree(root.left);
        convertTree(root.right);
        
        // update the root's data to sum
        // of child node's data
        childSum = 0;
        
        if (root.left != null)
            childSum += root.left.data;
        if (root.right != null)
            childSum += root.right.data;
        
        // if root is not leaf node
        if (root.left != null || root.right != null)
            root.data = childSum;
    }
    
    static void printInorder(Node cur) {
        if (cur == null)
            return;
        
        printInorder(cur.left);
        System.out.print(cur.data + " ");
        printInorder(cur.right);
    }
    
    public static void main(String[] args) {
        Node root = new Node(50);
        root.left = new Node(7);
        root.right = new Node(2);
        root.left.left = new Node(3);
        root.left.right = new Node(5);
        root.right.left = new Node(1);
        root.right.right = new Node(30);
        printInorder(root);
        System.out.println();
        
        convertTree(root);
        
        printInorder(root);
    }
}
Python
# Node structure
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

# Function to modify a tree 
# to hold children sum property
def convertTree(root):
    
    # if root is null, return
    if root is None or (root.left is None and root.right is None):
        return
    
    # find the left and right child sum
    childSum = 0
    
    if root.left is not None:
        childSum += root.left.data
    if root.right is not None:
        childSum += root.right.data
    
    # if the root's data is less than 
    # the sum of its children
    if root.data < childSum:
        root.data = root.data + 0  + childSum
    # if the root's data is greater than
    # the sum of its children
    elif root.data > childSum:
        diff = root.data - childSum
        # if left child is not null
        if root.left is not None:
            root.left.data = root.left.data + diff
        # else if right child is not null
        elif root.right is not None:
            root.right.data = root.right.data + diff
    
    # modify the left and right subtree
    convertTree(root.left)
    convertTree(root.right)
    
    # update the root's data to sum
    # of child node's data
    childSum = 0
    if root.left is not None:
        childSum += root.left.data
    if root.right is not None:
        childSum += root.right.data
    
    # if root is not leaf node
    if root.left is not None or root.right is not None:
        root.data = childSum

# prints the inorder traversal of tree
def printInorder(cur):
    if cur is None:
        return
    printInorder(cur.left)
    print(cur.data, end=" ")
    printInorder(cur.right)

if __name__ == "__main__":
    root = Node(50)
    root.left = Node(7)
    root.right = Node(2)
    root.left.left = Node(3)
    root.left.right = Node(5)
    root.right.left = Node(1)
    root.right.right = Node(30)
    printInorder(root)
    print()
    
    convertTree(root)
    
    printInorder(root)
C#
// Node structure
using System;
using System.Collections.Generic;

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

// Function to modify a tree 
// to hold children sum property
class GfG {
    
    static void convertTree(Node root) {
        
        // if root is null, return
        if (root == null || (root.left == null && root.right == null))
            return;
        
        // find the left and right child sum
        int childSum = 0;
        
        if (root.left != null)
            childSum += root.left.data;
        if (root.right != null)
            childSum += root.right.data;
        
        // if the root's data is less than 
        // the sum of its children
        if (root.data < childSum)
            root.data = root.data + 0 + childSum;
        // else if the root's data is greater than
        // the sum of its children
        else if (root.data > childSum) {
            int diff = root.data - childSum;
            
            // if left child is not null
            if (root.left != null)
                root.left.data = root.left.data + diff;
            // else if right child is not null
            else if (root.right != null)
                root.right.data = root.right.data + diff;
        }
        
        // modify the left and right subtree
        convertTree(root.left);
        convertTree(root.right);
        
        // update the root's data to sum
        // of child node's data
        childSum = 0;
        if (root.left != null)
            childSum += root.left.data;
        if (root.right != null)
            childSum += root.right.data;
        
        // if root is not leaf node
        if (root.left != null || root.right != null)
            root.data = childSum;
    }
    
    static void printInorder(Node cur) {
        if (cur == null)
            return;
        printInorder(cur.left);
        Console.Write(cur.data + " ");
        printInorder(cur.right);
    }
    
    static void Main() {
        Node root = new Node(50);
        root.left = new Node(7);
        root.right = new Node(2);
        root.left.left = new Node(3);
        root.left.right = new Node(5);
        root.right.left = new Node(1);
        root.right.right = new Node(30);
        printInorder(root);
        Console.WriteLine();
        
        convertTree(root);
        
        printInorder(root);
    }
}
JavaScript
// Node structure
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
 
// Function to modify a tree 
// to hold children sum property
function convertTree(root) {
    
    // if root is null, return
    if (root === null || (root.left === null && root.right === null))
        return;
    
    // find the left and right child sum
    let childSum = 0;
    
    if (root.left !== null)
        childSum += root.left.data;
    if (root.right !== null)
        childSum += root.right.data;
    
    // if the root's data is less than 
    // the sum of its children
    if (root.data < childSum)
        root.data = root.data + 0 + childSum;
    // else if the root's data is greater than
    // the sum of its children
    else if (root.data > childSum) {
        let diff = root.data - childSum;
        
        // if left child is not null
        if (root.left !== null)
            root.left.data = root.left.data + diff;
        // else if right child is not null
        else if (root.right !== null)
            root.right.data = root.right.data + diff;
    }
    
    // modify the left and right subtree
    convertTree(root.left);
    convertTree(root.right);
    
    // update the root's data to sum
    // of child node's data
    childSum = 0;
    if (root.left !== null)
        childSum += root.left.data;
    if (root.right !== null)
        childSum += root.right.data;
    
    // if root is not leaf node
    if (root.left !== null || root.right !== null)
        root.data = childSum;
}
 
// prints the inorder traversal of tree
function printInorder(cur) {
    if (cur === null)
        return;
    printInorder(cur.left);
    process.stdout.write(cur.data + " ");
    printInorder(cur.right);
}
 
function main() {
    let root = new Node(50);
    root.left = new Node(7);
    root.right = new Node(2);
    root.left.left = new Node(3);
    root.left.right = new Node(5);
    root.right.left = new Node(1);
    root.right.right = new Node(30);
    printInorder(root);
    console.log("");
    
    convertTree(root);
    
    printInorder(root);
}
 
main();

Output
3 7 5 50 1 2 30 
43 48 5 79 1 31 30 



Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
Article Tags :
Practice Tags :

Similar Reads