Given a Binary Tree consisting of N nodes, the task is to find the sum of Bitwise AND of the sum of all leaf nodes and the sum of all non-leaf nodes for each level in the given Tree.
Examples:
Input: Below is the given tree:
5
/ \
3 9
/ \
6 4
\
7
Output: 5
Explanation:

For Level 1: leaf node sum = 0, non-leaf node sum = 5. So, 0&5 = 0.
For Level 2: leaf node sum = 9, non-leaf node sum = 3. So, 9&3 = 1.
For Level 3: leaf node sum = 4, non-leaf node sum = 6. So, 6&4 = 4.
For Level 4: leaf node sum = 7, non-leaf node sum = 0. So, 0&7 = 0.
Hence, the total sum is 0 + 1 + 4 + 0 = 5.
Input: Below is the given tree:
4
/ \
9 3
/ \
5 3
Output: 1
Explanation:
For Level 1: leaf node sum = 0, non-leaf node sum = 4. So, 0&4 = 0
For Level 2: leaf node sum = 9, non-leaf node sum = 3. So, 9&3 = 1
For Level 3: leaf node sum = 8, non-leaf node sum = 0. So, 8&0 = 0
Hence, the total sum is 0 + 1 + 0 = 1
Approach: The idea to solve the above problem is to perform the Level Order Traversal of the tree. While doing traversal, process nodes of different levels separately and for every level being processed, find the sum of leaf nodes and non-leaf nodes for each level. Follow the steps below to solve the problem:
- Initialize a queue Q and push the root node to it and initialize ans as 0 to store the required answer.
- Perform the following steps till Q is not empty:
- Initialize leafSum as 0 and nonLeafSum as 0 to store the sum of leaf nodes and non-leaf nodes at the current level respectively.
- Find the current size of the queue Q and let it be L.
- Iterate over the range [0, L] and do the following:
- Pop the node from the queue.
- If the popped node is a leaf node, then add the value to leafSum else add it to nonLeafSum.
- Push the left and right child of the current popped node into the queue if they exist.
- For the current level add the Bitwise AND of leafSum and nonLeafSum to the variable ans.
- 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 TreeNode
{
int val;
TreeNode *left,*right;
TreeNode( int x = 0)
{
val = x;
left = NULL;
right = NULL;
}
};
int findSum(TreeNode *root){
queue<TreeNode*> que;
que.push(root);
int ans = 0;
while (que.size())
{
int leaf = 0;
int nonleaf = 0;
int length = que.size();
while (length)
{
auto temp = que.front();
que.pop();
if (!temp->left && !temp->right)
leaf += temp->val;
else
nonleaf += temp->val;
if (temp->left)
que.push(temp->left);
if (temp->right)
que.push(temp->right);
length -= 1;
}
ans += leaf & nonleaf;
}
return ans;
}
int main()
{
TreeNode *root = new TreeNode(5);
root->left = new TreeNode(3);
root->right = new TreeNode(9);
root->left->left = new TreeNode(6);
root->left->right = new TreeNode(4);
root->left->left->right = new TreeNode(7);
cout<<findSum(root);
}
|
Java
import java.util.*;
class GFG
{
static class TreeNode
{
int val;
TreeNode left,right;
TreeNode( int x)
{
val = x;
left = null ;
right = null ;
}
};
static int findSum(TreeNode root)
{
Queue<TreeNode> que = new LinkedList<>();
que.add(root);
int ans = 0 ;
while (que.size() > 0 )
{
int leaf = 0 ;
int nonleaf = 0 ;
int length = que.size();
while (length> 0 )
{
TreeNode temp = que.peek();
que.remove();
if (temp.left == null && temp.right == null )
leaf += temp.val;
else
nonleaf += temp.val;
if (temp.left != null )
que.add(temp.left);
if (temp.right != null )
que.add(temp.right);
length -= 1 ;
}
ans += leaf & nonleaf;
}
return ans;
}
public static void main(String[] args)
{
TreeNode root = new TreeNode( 5 );
root.left = new TreeNode( 3 );
root.right = new TreeNode( 9 );
root.left.left = new TreeNode( 6 );
root.left.right = new TreeNode( 4 );
root.left.left.right = new TreeNode( 7 );
System.out.print(findSum(root));
}
}
|
Python3
class TreeNode:
def __init__( self , val = 0 , left = None , right = None ):
self .val = val
self .left = left
self .right = right
def findSum(root):
que = [root]
ans = 0
while ( len (que)):
leaf = 0
nonleaf = 0
length = len (que)
while length:
temp = que.pop( 0 )
if not temp.left and not temp.right:
leaf + = temp.val
else :
nonleaf + = temp.val
if temp.left:
que.append(temp.left)
if temp.right:
que.append(temp.right)
length - = 1
ans + = leaf & nonleaf
return ans
root = TreeNode( 5 )
root.left = TreeNode( 3 )
root.right = TreeNode( 9 )
root.left.left = TreeNode( 6 )
root.left.right = TreeNode( 4 )
root.left.left.right = TreeNode( 7 )
print (findSum(root))
|
C#
using System;
using System.Collections.Generic;
class GFG{
class TreeNode
{
public int val;
public TreeNode left,right;
};
static TreeNode newNode( int x)
{
TreeNode temp = new TreeNode();
temp.val = x;
temp.left = null ;
temp.right = null ;
return temp;
}
static int findSum(TreeNode root){
Queue<TreeNode> que = new Queue<TreeNode>();
que.Enqueue(root);
int ans = 0;
while (que.Count>0)
{
int leaf = 0;
int nonleaf = 0;
int length = que.Count;
while (length>0)
{
TreeNode temp = que.Peek();
que.Dequeue();
if (temp.left == null && temp.right== null )
leaf += temp.val;
else
nonleaf += temp.val;
if (temp.left!= null )
que.Enqueue(temp.left);
if (temp.right != null )
que.Enqueue(temp.right);
length -= 1;
}
ans += (leaf & nonleaf);
}
return ans;
}
public static void Main()
{
TreeNode root = newNode(5);
root.left = newNode(3);
root.right = newNode(9);
root.left.left = newNode(6);
root.left.right = newNode(4);
root.left.left.right = newNode(7);
Console.WriteLine(findSum(root));
}
}
|
Javascript
<script>
class TreeNode
{
constructor(x)
{
this .val = x;
this .left = null ;
this .right = null ;
}
}
function findSum(root)
{
let que = [];
que.push(root);
let ans = 0;
while (que.length > 0)
{
let leaf = 0;
let nonleaf = 0;
let length = que.length;
while (length > 0)
{
let temp = que.shift();
if (temp.left == null &&
temp.right == null )
leaf += temp.val;
else
nonleaf += temp.val;
if (temp.left != null )
que.push(temp.left);
if (temp.right != null )
que.push(temp.right);
length -= 1;
}
ans += leaf & nonleaf;
}
return ans;
}
let root = new TreeNode(5);
root.left = new TreeNode(3);
root.right = new TreeNode(9);
root.left.left = new TreeNode(6);
root.left.right = new TreeNode(4);
root.left.left.right = new TreeNode(7);
document.write(findSum(root));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)