Convert a given Binary tree to a tree that holds Logical AND property | Program to convert an aribitary binary tree to a tree that holds children sum property Helper function that allocates a new node with the given data and None left and right poers . ; Construct to create a new node ; Convert the given tree to a tree where each node is logical AND of its children The main idea is to do Postorder traversal ; first recur on left child ; then recur on right child ; first recur on left child ; then print the data of node ; now recur on right child ; Driver Code ; Create following Binary Tree 1 / \ 1 0 / \ / \ 0 1 1 1
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def convertTree ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT convertTree ( root . left ) NEW_LINE convertTree ( root . right ) NEW_LINE if ( root . left != None and root . right != None ) : NEW_LINE INDENT root . data = ( ( root . left . data ) & ( root . right . data ) ) NEW_LINE DEDENT DEDENT def printInorder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT printInorder ( root . left ) NEW_LINE print ( root . data , end = " ▁ " ) NEW_LINE printInorder ( root . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 0 ) NEW_LINE root . left = newNode ( 1 ) NEW_LINE root . right = newNode ( 0 ) NEW_LINE root . left . left = newNode ( 0 ) NEW_LINE root . left . right = newNode ( 1 ) NEW_LINE root . right . left = newNode ( 1 ) NEW_LINE root . right . right = newNode ( 1 ) NEW_LINE print ( " Inorder ▁ traversal ▁ before ▁ conversion " , end = " ▁ " ) NEW_LINE printInorder ( root ) NEW_LINE convertTree ( root ) NEW_LINE print ( " Inorder traversal after conversion " , ▁ end ▁ = ▁ "   " ) NEW_LINE printInorder ( root ) NEW_LINE DEDENT