Convert an arbitrary Binary Tree to a tree that holds Children Sum Property – Set 2
Last Updated :
21 Dec, 2021
Question: Given an arbitrary binary tree, convert it to a binary tree that holds Children Sum Property. You can only increment data values in any node (You cannot change the structure of the tree and cannot decrement the value of any node).
For example, the below tree doesn’t hold the children’s sum property, convert it to a tree that holds the property.
50
/ \
/ \
7 2
/ \ /\
/ \ / \
3 5 1 30
Naive Approach: The Naive Approach is discussed in the Set 1 of this article. Here, we are discussing the optimized approach.
Algorithm: Convert the children to the maximum possible value so while moving back there will be no parent having more value than the children, so there will be no extra function to again traverse the subtrees from that node.
- If the sum of children is less than current node, replace the value of both children with current node’s value.
if(node->left + node->right < node->data)
put node->data value in both the child
- If the sum of children is greater than or equal to current node, replace the value of current node’s value with sum of children.
if(node->left->data + node->right->data >= node->data)
put summation of both child data values in node->data
- While traversing back overwrite the existing node values with the sum of left and right child data.
(Note: there will not be any case where the value of node will be greater than the sum of values of their child, because we have given them the maximum possible value).
Follow the steps below to solve the problem:
- If root is null then return.
- Initialize the variable childSum as 0.
- If root has children, then add their value to childSum.
- If childSum is greater than equal to root->data, then set root->data as childSum.
- Else, if there are children of root, then set their children’s data as root->data.
- Call the same function for root->left and root->right.
- Initialize the variable totalSumToChangeParent as 0.
- If root has children, then add the value of their data to the variable totalSumToChangeParent.
- If there is any child of root, then set the value of root as totalSumToChangeParent.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node( int x)
{
data = x;
left = NULL;
right = NULL;
}
};
void convertTree(Node* root)
{
if (root == NULL)
return ;
int childSum = 0;
if (root->left)
childSum += root->left->data;
if (root->right)
childSum += root->right->data;
if (childSum >= root->data)
root->data = childSum;
else {
if (root->left)
root->left->data = root->data;
if (root->right)
root->right->data = root->data;
}
convertTree(root->left);
convertTree(root->right);
int totalSumToChangeParent = 0;
if (root->left)
totalSumToChangeParent
+= root->left->data;
if (root->right)
totalSumToChangeParent
+= root->right->data;
if (root->left || root->right)
root->data = totalSumToChangeParent;
}
void printInorder(Node* root)
{
if (root == NULL)
return ;
printInorder(root->left);
cout << root->data << " " ;
printInorder(root->right);
}
int 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);
convertTree(root);
printInorder(root);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node {
int data;
Node left;
Node right;
Node( int x)
{
data = x;
left = null ;
right = null ;
}
};
static void convertTree(Node root)
{
if (root == null )
return ;
int childSum = 0 ;
if (root.left!= null )
childSum += root.left.data;
if (root.right!= null )
childSum += root.right.data;
if (childSum >= root.data)
root.data = childSum;
else {
if (root.left!= null )
root.left.data = root.data;
if (root.right!= null )
root.right.data = root.data;
}
convertTree(root.left);
convertTree(root.right);
int totalSumToChangeParent = 0 ;
if (root.left != null )
totalSumToChangeParent
+= root.left.data;
if (root.right != null )
totalSumToChangeParent
+= root.right.data;
if (root.left != null || root.right != null )
root.data = totalSumToChangeParent;
}
static void printInorder(Node root)
{
if (root == null )
return ;
printInorder(root.left);
System.out.print(root.data+ " " );
printInorder(root.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 );
convertTree(root);
printInorder(root);
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self .left = None
self .right = None
def convertTree(root):
if (root = = None ):
return
childSum = 0
if (root.left):
childSum + = root.left.data
if (root.right):
childSum + = root.right.data
if (childSum > = root.data):
root.data = childSum
else :
if (root.left):
root.left.data = root.data
if (root.right):
root.right.data = root.data
convertTree(root.left)
convertTree(root.right)
totalSumToChangeParent = 0
if (root.left):
totalSumToChangeParent + = root.left.data
if (root.right):
totalSumToChangeParent + = root.right.data
if (root.left or root.right):
root.data = totalSumToChangeParent
def printInorder(root):
if (root = = None ):
return
printInorder(root.left)
print (root.data, end = " " )
printInorder(root.right)
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 )
convertTree(root)
printInorder(root)
|
C#
using System;
public class GFG{
class Node {
public int data;
public Node left;
public Node right;
public Node( int x)
{
data = x;
left = null ;
right = null ;
}
};
static void convertTree(Node root)
{
if (root == null )
return ;
int childSum = 0;
if (root.left!= null )
childSum += root.left.data;
if (root.right!= null )
childSum += root.right.data;
if (childSum >= root.data)
root.data = childSum;
else {
if (root.left!= null )
root.left.data = root.data;
if (root.right!= null )
root.right.data = root.data;
}
convertTree(root.left);
convertTree(root.right);
int totalSumToChangeParent = 0;
if (root.left != null )
totalSumToChangeParent
+= root.left.data;
if (root.right != null )
totalSumToChangeParent
+= root.right.data;
if (root.left != null || root.right != null )
root.data = totalSumToChangeParent;
}
static void printInorder(Node root)
{
if (root == null )
return ;
printInorder(root.left);
Console.Write(root.data+ " " );
printInorder(root.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);
convertTree(root);
printInorder(root);
}
}
|
Javascript
<script>
class Node {
constructor(x) {
this .data = x;
this .left = null ;
this .right = null ;
}
};
function convertTree(root) {
if (root == null )
return ;
let childSum = 0;
if (root.left)
childSum += root.left.data;
if (root.right)
childSum += root.right.data;
if (childSum >= root.data)
root.data = childSum;
else {
if (root.left)
root.left.data = root.data;
if (root.right)
root.right.data = root.data;
}
convertTree(root.left);
convertTree(root.right);
let totalSumToChangeParent = 0;
if (root.left)
totalSumToChangeParent
+= root.left.data;
if (root.right)
totalSumToChangeParent
+= root.right.data;
if (root.left || root.right)
root.data = totalSumToChangeParent;
}
function printInorder(root) {
if (root == null )
return ;
printInorder(root.left);
document.write(root.data + " " );
printInorder(root.right);
}
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);
convertTree(root);
printInorder(root);
</script>
|
Output:
50 100 50 200 50 100 50
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...