Print the number of set bits in each node of a Binary Tree
Last Updated :
29 Nov, 2022
Given a Binary Tree. The task is to print the number of set bits in each of the nodes in the Binary Tree.

The idea is to traverse the given binary tree using any tree traversal method, and for each node calculate the number of set bits and print it.
Note: One can also use the __builtin_popcount() function available in C++ to directly count the number of set bits in an integer.
Below is the implementation of the above approach:
C++
// CPP program to print the number of set bits
// in each node of the binary tree
#include <bits/stdc++.h>
using namespace std;
// Binary Tree node
struct Node {
int data;
struct Node *left, *right;
};
// Utility function that allocates a new Node
Node* newNode(int data)
{
Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
// Function to print the number of set bits
// in each node of the binary tree
void printSetBit(Node* root)
{
if (root == NULL)
return;
// Print the number of set bits of
// current node using __builtin_popcount()
cout << "Set bits in Node " << root->data << " = " <<
__builtin_popcount(root->data) << "\n";
// Traverse Left Subtree
printSetBit(root->left);
// Traverse Right Subtree
printSetBit(root->right);
}
// Driver code
int main()
{
Node* root = newNode(16);
root->left = newNode(13);
root->left->left = newNode(14);
root->left->right = newNode(12);
root->right = newNode(11);
root->right->left = newNode(10);
root->right->right = newNode(16);
printSetBit(root);
return 0;
}
Java
// Java program to print the number of set bits
// in each node of the binary tree
import java.util.*;
class GFG
{
// Binary Tree node
static class Node
{
int data;
Node left, right;
};
// Utility function that allocates a new Node
static Node newNode(int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null;
return (node);
}
// Function to print the number of set bits
// in each node of the binary tree
static void printSetBit(Node root)
{
if (root == null)
return;
// Print the number of set bits of
// current node using __builtin_popcount()
System.out.print("Set bits in Node " + root.data + " = " +
Integer.bitCount(root.data) + "\n");
// Traverse Left Subtree
printSetBit(root.left);
// Traverse Right Subtree
printSetBit(root.right);
}
// Driver code
public static void main(String[] args)
{
Node root = newNode(16);
root.left = newNode(13);
root.left.left = newNode(14);
root.left.right = newNode(12);
root.right = newNode(11);
root.right.left = newNode(10);
root.right.right = newNode(16);
printSetBit(root);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to print the number of set bits
# in each node of the binary tree
# Binary Tree node
class Node:
def __init__(self):
self.data = None
self.left = None
self.right = None
# Utility function that allocates a new Node
def newNode(data):
node = Node()
node.data = data
node.left = None
node.right = None
return node
# Function to print the number of set bits
# in each node of the binary tree
def printSetBit(root: Node):
if root is None:
return
# Print the number of set bits of
# current node using count()
print("Set bits in Node %d = %d" %
(root.data, bin(root.data).count('1')))
# Traverse Left Subtree
printSetBit(root.left)
# Traverse Right Subtree
printSetBit(root.right)
# Driver Code
if __name__ == "__main__":
root = newNode(16)
root.left = newNode(13)
root.left.left = newNode(14)
root.left.right = newNode(12)
root.right = newNode(11)
root.right.left = newNode(10)
root.right.right = newNode(16)
printSetBit(root)
# This code is contributed by
# sanjeev2552
C#
// C# program to print the number of set bits
// in each node of the binary tree
using System;
class GFG
{
// Binary Tree node
public class Node
{
public int data;
public Node left, right;
};
// Utility function that allocates a new Node
static Node newNode(int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null;
return (node);
}
// Function to print the number of set bits
// in each node of the binary tree
static void printSetBit(Node root)
{
if (root == null)
return;
// Print the number of set bits of
// current node using __builtin_popcount()
Console.Write("Set bits in Node " + root.data +
" = " + bitCount(root.data) + "\n");
// Traverse Left Subtree
printSetBit(root.left);
// Traverse Right Subtree
printSetBit(root.right);
}
static int bitCount(int x)
{
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
// Driver code
public static void Main(String[] args)
{
Node root = newNode(16);
root.left = newNode(13);
root.left.left = newNode(14);
root.left.right = newNode(12);
root.right = newNode(11);
root.right.left = newNode(10);
root.right.right = newNode(16);
printSetBit(root);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to print the number
// of set bits in each node of the binary tree
// A Binary Tree Node
class Node
{
constructor(data)
{
this.left = null;
this.right = null;
this.data = data;
}
}
// Utility function that allocates a new Node
function newNode(data)
{
let node = new Node(data);
return (node);
}
function bitCount(x)
{
let setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
// Function to print the number of set bits
// in each node of the binary tree
function printSetBit(root)
{
if (root == null)
return;
// Print the number of set bits of
// current node using __builtin_popcount()
document.write("Set bits in Node " + root.data +
" = " + bitCount(root.data) + "</br>");
// Traverse Left Subtree
printSetBit(root.left);
// Traverse Right Subtree
printSetBit(root.right);
}
// Driver code
let root = newNode(16);
root.left = newNode(13);
root.left.left = newNode(14);
root.left.right = newNode(12);
root.right = newNode(11);
root.right.left = newNode(10);
root.right.right = newNode(16);
printSetBit(root);
// This code is contributed by suresh07
</script>
OutputSet bits in Node 16 = 1
Set bits in Node 13 = 3
Set bits in Node 14 = 3
Set bits in Node 12 = 2
Set bits in Node 11 = 3
Set bits in Node 10 = 2
Set bits in Node 16 = 1
Time Complexity: O(N × log2(max_node)), where N is the number of nodes, and max_node is the node with the maximum value.
Auxiliary Space: O(1)
Similar Reads
Number of full binary trees such that each node is product of its children Given an array of n integers, each integer is greater than 1. The task is to find the number of Full binary tree from the given integers, such that each non-leaf node value is the product of its children value. Given that, each integer can be used multiple times in a full binary tree. Examples: Inpu
11 min read
Count the number of Nodes in a Binary Tree in Constant Space Given a binary tree having N nodes, count the number of nodes using constant O(1) space. This can be done by simple traversals like- Preorder, InOrder, PostOrder, and LevelOrder but these traversals require an extra space which is equal to the height of the tree. Examples: Input: Output: 5Explanatio
10 min read
Count the Number of Binary Search Trees present in a Binary Tree Given a binary tree, the task is to count the number of Binary Search Trees present in it. Examples: Input: 1 / \ 2 3 / \ / \ 4 5 6 7 Output: 4Here each leaf node represents a binary search tree and there are total 4 nodes. Input: 11 / \ 8 10 / / \ 5 9 8 / \ 4 6 Output: 6 Sub-tree rooted under node
10 min read
Number of leaf nodes in the subtree of every node of an n-ary tree Given an N-ary tree, print the number of leaf nodes in the subtree of every node. Examples: Input: 1 / \ 2 3 / | \ 4 5 6 Output: The node 1 has 4 leaf nodes The node 2 has 1 leaf nodes The node 3 has 3 leaf nodes The node 4 has 1 leaf nodes The node 5 has 1 leaf nodes The node 6 has 1 leaf nodes App
8 min read
Maximize count of set bits in a root to leaf path in a binary tree 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: 12Explanation:Path 1: 15(1111)->3(0011)->5(0101) = 8Path 2: 15(1111)->3(0011)->1(0001) = 7Path 3: 15(01111)-
6 min read