Print the number of set bits in each node of a Binary Tree
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 |
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 |
Set 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
Recommended Posts:
- Print cousins of a given node in Binary Tree
- Print Ancestors of a given node in Binary Tree
- Print path from root to a given node in a binary tree
- Print the nodes of binary tree as they become the leaf node
- Print cousins of a given node in Binary Tree | Single Traversal
- Print all the nodes except the leftmost node in every level of the given binary tree
- Number of turns to reach from one node to other in binary tree
- K-th ancestor of a node in Binary Tree | Set 3
- Search a node in Binary Tree
- Kth ancestor of a node in binary tree | Set 2
- Get Level of a node in a Binary Tree
- Sum of cousins of a given node in a Binary Tree
- K-th ancestor of a node in Binary Tree
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Get last node of the binary tree following given pattern starting from X
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



