Print the number of set bits in each node of a Binary Tree | C # program to print the number of set bits in each node of the binary tree ; Binary Tree node ; Utility function that allocates a new Node ; Function to print the number of set bits in each node of the binary tree ; Print the number of set bits of current node using __builtin_popcount ( ) ; Traverse Left Subtree ; Traverse Right Subtree ; Driver code
using System ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void printSetBit ( Node root ) { if ( root == null ) return ; Console . Write ( " Set ▁ bits ▁ in ▁ Node ▁ " + root . data + " ▁ = ▁ " + bitCount ( root . data ) + " STRNEWLINE " ) ; printSetBit ( root . left ) ; printSetBit ( root . right ) ; } static int bitCount ( int x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } 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 ) ; } }