Print the number of set bits in each node of a 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 count ( ) ; Traverse Left Subtree ; Traverse Right Subtree ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = None NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT node = Node ( ) NEW_LINE node . data = data NEW_LINE node . left = None NEW_LINE node . right = None NEW_LINE return node NEW_LINE DEDENT def printSetBit ( root : Node ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT print ( " Set ▁ bits ▁ in ▁ Node ▁ % d ▁ = ▁ % d " % ( root . data , bin ( root . data ) . count ( '1' ) ) ) NEW_LINE printSetBit ( root . left ) NEW_LINE printSetBit ( root . right ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = newNode ( 16 ) NEW_LINE root . left = newNode ( 13 ) NEW_LINE root . left . left = newNode ( 14 ) NEW_LINE root . left . right = newNode ( 12 ) NEW_LINE root . right = newNode ( 11 ) NEW_LINE root . right . left = newNode ( 10 ) NEW_LINE root . right . right = newNode ( 16 ) NEW_LINE printSetBit ( root ) NEW_LINE DEDENT