Sum of nodes having sum of subtrees of opposite parities
Last Updated :
30 May, 2022
Given a Binary Tree, the task is to find the sum of all such nodes from the given tree whose sum of left and right subtree is either odd and even or even and odd respectively.
Examples:
Input:
11
/ \
23 44
/ \ / \
13 9 22 7
/ \
6 15
Output: 33
Explanation: There are only two such nodes:
- Node 22 having left subtree and right subtree sum as 6 (even) and 15(odd).
- Node 11 having left subtree and right subtree sum as 45 (odd) and 94 (even).
Therefore, the total sum = 22 + 11 = 33.
Input:
11
/
5
/ \
3 1
Output: 0
Explanation: There is no such node satisfying the given condition.
Approach: The idea is to recursively calculate the sum of the left subtree and the sum of the right subtree and then check for the given condition. Follow the steps below to solve the problem:
- Initialize a variable ans as 0 to store the sum of all such nodes.
- Perform the PostOrder Traversal in the given Tree.
- Find the sum of left and right subtree for each node and check if the sum are non-zero and check if sum of both the sums is odd or not. If found to be true, then include the current node value in ans.
- Return the sum of all the nodes of left subtree, right subtree, and current node value in each recursive calls.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
struct Node* newNode( int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
int mSum;
int getSum(Node *root)
{
if (root == NULL)
return 0;
int lSum = getSum(root->left);
int rSum = getSum(root->right);
if (lSum != 0 && rSum != 0)
if ((lSum + rSum) % 2 != 0)
mSum += root->data;
return lSum + rSum + root->data;
}
int main()
{
int n = 9;
struct Node *root = newNode(11);
root->left = newNode(23);
root->right = newNode(44);
root->left->left = newNode(13);
root->left->right = newNode(9);
root->right->left = newNode(22);
root->right->right = newNode(7);
root->right->left->left = newNode(6);
root->right->left->right = newNode(15);
mSum = 0;
getSum(root);
cout<<(mSum);
}
|
Java
import java.util.*;
import java.lang.*;
class Node {
int data;
Node left, right;
Node( int item)
{
data = item;
left = right = null ;
}
}
class BinaryTree {
static int mSum;
Node root;
static int getSum(Node root)
{
if (root == null )
return 0 ;
int lSum = getSum(root.left);
int rSum = getSum(root.right);
if (lSum != 0 && rSum != 0 )
if ((lSum + rSum) % 2 != 0 )
mSum += root.data;
return lSum + rSum + root.data;
}
public static void main(String[] args)
{
int n = 9 ;
BinaryTree tree = new BinaryTree();
tree.root = new Node( 11 );
tree.root.left = new Node( 23 );
tree.root.right = new Node( 44 );
tree.root.left.left = new Node( 13 );
tree.root.left.right = new Node( 9 );
tree.root.right.left = new Node( 22 );
tree.root.right.right = new Node( 7 );
tree.root.right.left.left = new Node( 6 );
tree.root.right.left.right = new Node( 15 );
mSum = 0 ;
getSum(tree.root);
System.out.println(mSum);
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self .left = None
self .right = None
mSum = 0
def getSum(root):
global mSum
if (root = = None ):
return 0
lSum = getSum(root.left)
rSum = getSum(root.right)
if (lSum ! = 0 and rSum ! = 0 ):
if ((lSum + rSum) % 2 ! = 0 ):
mSum + = root.data
return lSum + rSum + root.data
if __name__ = = '__main__' :
n = 9
root = Node( 11 )
root.left = Node( 23 )
root.right = Node( 44 )
root.left.left = Node( 13 )
root.left.right = Node( 9 )
root.right.left = Node( 22 )
root.right.right = Node( 7 )
root.right.left.left = Node( 6 )
root.right.left.right = Node( 15 )
mSum = 0
getSum(root)
print (mSum)
|
C#
using System;
public class Node
{
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
class BinaryTree{
static int mSum;
Node root;
static int getSum(Node root)
{
if (root == null )
return 0;
int lSum = getSum(root.left);
int rSum = getSum(root.right);
if (lSum != 0 && rSum != 0)
if ((lSum + rSum) % 2 != 0)
mSum += root.data;
return lSum + rSum + root.data;
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(11);
tree.root.left = new Node(23);
tree.root.right = new Node(44);
tree.root.left.left = new Node(13);
tree.root.left.right = new Node(9);
tree.root.right.left = new Node(22);
tree.root.right.right = new Node(7);
tree.root.right.left.left = new Node(6);
tree.root.right.left.right = new Node(15);
mSum = 0;
getSum(tree.root);
Console.WriteLine(mSum);
}
}
|
Javascript
<script>
class Node
{
constructor(item)
{
this .left = null ;
this .right = null ;
this .data = item;
}
}
let mSum;
let root;
class BinaryTree
{
constructor()
{
this .root = null ;
}
}
function getSum(root)
{
if (root == null )
return 0;
let lSum = getSum(root.left);
let rSum = getSum(root.right);
if (lSum != 0 && rSum != 0)
if ((lSum + rSum) % 2 != 0)
mSum += root.data;
return lSum + rSum + root.data;
}
let n = 9;
let tree = new BinaryTree();
tree.root = new Node(11);
tree.root.left = new Node(23);
tree.root.right = new Node(44);
tree.root.left.left = new Node(13);
tree.root.left.right = new Node(9);
tree.root.right.left = new Node(22);
tree.root.right.right = new Node(7);
tree.root.right.left.left = new Node(6);
tree.root.right.left.right = new Node(15);
mSum = 0;
getSum(tree.root);
document.write(mSum);
</script>
|
Time Complexity: O(N), where N is the number of nodes in the Binary Tree.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...