Sum of all parent-child differences in a Binary Tree
Last Updated :
16 Aug, 2022
Given a binary tree, find the sum of all parent-child differences for all the non-leaf nodes of the given binary tree.
Note that parent-child difference is (parent node’s value – (sum of child node’s values)).
Examples:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Output: -23
1st parent-child difference = 1 -(2 + 3) = -4
2nd parent-child difference = 2 -(4 + 5) = -7
3rd parent-child difference = 3 -(6 + 7) = -10
4th parent-child difference = 6 - 8 = -2
Total sum = -23
Input:
1
/ \
2 3
\ /
5 6
Output: -10
Naive Approach: The idea is to traverse the tree in any fashion and check if the node is the leaf node or not. If the node is non-leaf node, add (node data – sum of children node data) to result.
Efficient Approach: In the final result, a close analysis suggests that each internal node ( nodes which are neither root nor leaf) once gets treated as a child and once as a parent hence their contribution in the final result is zero. Also, the root is only treated as a parent once and in a similar fashion, all leaf nodes are treated as children once. Hence, the final result is (value of root – sum of all leaf nodes).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int data)
{
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
}
void leafSumFunc(Node* root, int * leafSum)
{
if (!root)
return ;
if (!root->left && !root->right)
*leafSum += root->data;
leafSumFunc(root->left, leafSum);
leafSumFunc(root->right, leafSum);
}
int sumParentChildDiff(Node* root)
{
if (!root)
return 0;
if (!root->left && !root->right)
return root->data;
int leafSum = 0;
leafSumFunc(root, &leafSum);
return (root->data - leafSum);
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right = newNode(3);
root->right->right = newNode(7);
root->right->left = newNode(6);
cout << sumParentChildDiff(root);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node left, right;
};
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
static int leafSum;
static void leafSumFunc(Node root )
{
if (root == null )
return ;
if (root.left == null && root.right == null )
leafSum += root.data;
leafSumFunc(root.left);
leafSumFunc(root.right);
}
static int sumParentChildDiff(Node root)
{
if (root == null )
return 0 ;
if (root.left == null && root.right == null )
return root.data;
leafSum = 0 ;
leafSumFunc(root);
return (root.data - leafSum);
}
public static void main(String args[])
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.left.left = newNode( 4 );
root.left.right = newNode( 5 );
root.right = newNode( 3 );
root.right.right = newNode( 7 );
root.right.left = newNode( 6 );
System.out.println( sumParentChildDiff(root));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def leafSumFunc(root, leafSum):
if not root:
return 0
if not root.left and not root.right:
leafSum + = root.data
leafSum = max (leafSumFunc(root.left,
leafSum), leafSum)
leafSum = max (leafSumFunc(root.right,
leafSum), leafSum)
return leafSum
def sumParentChildDiff(root):
if not root:
return 0
if not root.left and not root.right:
return root.data
leafSum = leafSumFunc(root, 0 )
return root.data - leafSum
if __name__ = = "__main__" :
root = Node( 1 )
root.left = Node( 2 )
root.left.left = Node( 4 )
root.left.right = Node( 5 )
root.right = Node( 3 )
root.right.right = Node( 7 )
root.right.left = Node( 6 )
print (sumParentChildDiff(root))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node left, right;
};
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
static int leafSum;
static void leafSumFunc(Node root )
{
if (root == null )
return ;
if (root.left == null && root.right == null )
leafSum += root.data;
leafSumFunc(root.left);
leafSumFunc(root.right);
}
static int sumParentChildDiff(Node root)
{
if (root == null )
return 0;
if (root.left == null && root.right == null )
return root.data;
leafSum = 0;
leafSumFunc(root);
return (root.data - leafSum);
}
public static void Main(String []args)
{
Node root = newNode(1);
root.left = newNode(2);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right = newNode(3);
root.right.right = newNode(7);
root.right.left = newNode(6);
Console.WriteLine( sumParentChildDiff(root));
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let temp = new Node(data);
return temp;
}
let leafSum;
function leafSumFunc(root)
{
if (root == null )
return ;
if (root.left == null && root.right == null )
leafSum += root.data;
leafSumFunc(root.left);
leafSumFunc(root.right);
}
function sumParentChildDiff(root)
{
if (root == null )
return 0;
if (root.left == null && root.right == null )
return root.data;
leafSum = 0;
leafSumFunc(root);
return (root.data - leafSum);
}
let root = newNode(1);
root.left = newNode(2);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right = newNode(3);
root.right.right = newNode(7);
root.right.left = newNode(6);
document.write( sumParentChildDiff(root));
</script>
|
Time complexity: O(N) where N is no of nodes in given binary tree
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...