Given a binary tree, the task is to find the total count of set bits in the node values of all the root to leaf paths and print the maximum among them.
Examples:
Input:

Output: 12
Explanation:
Path 1: 15(1111)->3(0011)->5(0101) = 8
Path 2: 15(1111)->3(0011)->1(0001) = 7
Path 3: 15(01111)->7(00111)->31(11111) = 12 (maximum)
Path 4: 15(1111)->7(0111)->9(1001) = 9
Therefore, the maximum count of set bits obtained in a path is 12.
Input:

Output: 13
Explanation:
Path 1: 31(11111)->3(00011)->7(00111) = 10
Path 2: 31(11111)->3(00011)->1(00001) = 8
Path 3: 31(11111)->15(01111)->5(00101) = 11
Path 4: 31(11111)->15(01111)->23(10111) = 13 (maximum)
Therefore, the maximum count of set bits obtained in a path is 13.
Approach:
Follow the steps below to solve the problem:
- Traverse each node recursively, starting from the root node
- Calculate the number of set bits in the value of the current node.
- Update the maximum count of set bits(stored in a variable, say maxm).
- Traverse its left and right subtree.
- After complete traversal of all the nodes of the tree, print the final value of maxm as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxm = 0;
struct Node {
int val;
Node *left, *right;
Node( int x)
{
val = x;
left = NULL;
right = NULL;
}
};
void maxm_setbits(Node* root, int ans)
{
if (!root)
return ;
if (root->left == NULL
&& root->right == NULL) {
ans += __builtin_popcount(root->val);
maxm = max(ans, maxm);
return ;
}
maxm_setbits(root->left,
ans + __builtin_popcount(
root->val));
maxm_setbits(root->right,
ans + __builtin_popcount(
root->val));
}
int main()
{
Node* root = new Node(15);
root->left = new Node(3);
root->right = new Node(7);
root->left->left = new Node(5);
root->left->right = new Node(1);
root->right->left = new Node(31);
root->right->right = new Node(9);
maxm_setbits(root, 0);
cout << maxm << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxm = 0 ;
static class Node
{
int val;
Node left, right;
Node( int x)
{
val = x;
left = null ;
right = null ;
}
};
static void maxm_setbits(Node root, int ans)
{
if (root == null )
return ;
if (root.left == null &&
root.right == null )
{
ans += Integer.bitCount(root.val);
maxm = Math.max(ans, maxm);
return ;
}
maxm_setbits(root.left,
ans + Integer.bitCount(
root.val));
maxm_setbits(root.right,
ans + Integer.bitCount(
root.val));
}
public static void main(String[] args)
{
Node root = new Node( 15 );
root.left = new Node( 3 );
root.right = new Node( 7 );
root.left.left = new Node( 5 );
root.left.right = new Node( 1 );
root.right.left = new Node( 31 );
root.right.right = new Node( 9 );
maxm_setbits(root, 0 );
System.out.print(maxm + "\n" );
}
}
|
Python3
maxm = 0
class Node:
def __init__( self , x):
self .val = x
self .left = None
self .right = None
def count_1(n):
count = 0
while (n):
count + = n & 1
n >> = 1
return count
def maxm_setbits(root, ans):
global maxm
if not root:
return
if (root.left = = None and
root.right = = None ):
ans + = count_1(root.val)
maxm = max (ans, maxm)
return
maxm_setbits(root.left,
ans + count_1(root.val))
maxm_setbits(root.right,
ans + count_1(root.val))
root = Node( 15 )
root.left = Node( 3 )
root.right = Node( 7 )
root.left.left = Node( 5 )
root.left.right = Node( 1 )
root.right.left = Node( 31 )
root.right.right = Node( 9 )
maxm_setbits(root, 0 )
print (maxm)
|
C#
using System;
class GFG{
static void sortArr( int []a, int n)
{
int i, k;
k = ( int )(Math.Log(n) / Math.Log(2));
k = ( int ) Math.Pow(2, k);
while (k > 0)
{
for (i = 0; i + k < n; i++)
if (a[i] > a[i + k])
{
int tmp = a[i];
a[i] = a[i + k];
a[i + k] = tmp;
}
k = k / 2;
}
for (i = 0; i < n; i++)
{
Console.Write(a[i] + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 5, 20, 30, 40, 36,
33, 25, 15, 10 };
int n = arr.Length;
sortArr(arr, n);
}
}
|
Javascript
<script>
let maxm = 0;
class Node
{
constructor(x)
{
this .val = x;
this .left = null ;
this .right = null ;
}
}
var root;
function maxm_setbits(root, ans)
{
if (!root)
return ;
if (root.left == null &&
root.right == null )
{
ans += (root.val).toString(2).split( '' ).filter(
y => y == '1' ).length;
maxm = Math.max(ans, maxm);
return ;
}
maxm_setbits(root.left,
ans + (root.val).toString(2).split( '' ).filter(
y => y == '1' ).length);
maxm_setbits(root.right,
ans + (root.val).toString(2).split( '' ).filter(
y => y == '1' ).length);
}
root = new Node(15);
root.left = new Node(3);
root.right = new Node(7);
root.left.left = new Node(5);
root.left.right = new Node(1);
root.right.left = new Node(31);
root.right.right = new Node(9);
maxm_setbits(root, 0);
document.write(maxm);
</script>
|
Time Complexity: O(N), where N denotes the number of nodes.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Aug, 2021
Like Article
Save Article