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:

`// 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; ` `} ` |

**Output:**

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:

- Accolite Interview Experience (On-campus)
- Find bitwise OR of all possible sub-arrays
- Find bitwise AND (&) of all possible sub-arrays
- Rotations of a Binary String with Odd Value
- Hamming distance between two Integers
- 2's compliment for a givin string using XOR
- Check whether bits are in alternate pattern in the given range | Set-2
- Minimum value of N such that xor from 1 to N is equal to K
- Program to invert bits of a number Efficiently
- Remove duplicates from a string in O(1) extra space
- Calculate the total fine to be collected
- Sum of bitwise OR of all subarrays
- Value in a given range with maximum XOR
- Check if a number can be expressed as 2^x + 2^y
- Find the missing element in an array of integers represented in binary format

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.