Given a Binary Tree. The task is to print the number of set bits in each of the nodes in the Binary Tree.
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:
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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print Ancestors of a given node in Binary Tree
- Print cousins 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 all the nodes except the leftmost node in every level of the given binary tree
- Print cousins of a given node in Binary Tree | Single Traversal
- Print all nodes except rightmost node of every level of the Binary Tree
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
- Number of turns to reach from one node to other in binary tree
- Print the nodes of the Binary Tree whose height is a Prime number
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Kth ancestor of a node in binary tree | Set 2
- Sum of cousins of a given node in a Binary Tree
- Search a node in Binary Tree
- K-th ancestor of a node in Binary Tree
- Get Level of a node in a Binary Tree
- K-th ancestor of a node in Binary Tree | Set 3
- Find the parent of a node in the given binary tree
- Kth node in Diagonal Traversal of Binary Tree
- Total sum except adjacent of a given node in a Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.