Remove all subtrees consisting only of even valued nodes from a Binary Tree | Node of the tree ; Function to print tree level wise ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root ; Print front of queue and remove it from queue ; If left child is present ; Otherwise ; If right child is present ; Otherwise ; Function to remove subtrees ; Base Case ; Search for required condition in left and right half ; If the node is even and leaf node ; Driver Code ; Function Call ; Print answer
class node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def printLevelOrder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE print ( temp . data , end = " ▁ " ) NEW_LINE q = q [ 1 : ] NEW_LINE if ( temp . left != None ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT elif ( temp . right != None ) : NEW_LINE INDENT print ( " NULL " , end = " ▁ " ) NEW_LINE DEDENT if ( temp . right != None ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT elif ( temp . left != None ) : NEW_LINE INDENT print ( " NULL " , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT def pruneTree ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT root . left = pruneTree ( root . left ) NEW_LINE root . right = pruneTree ( root . right ) NEW_LINE if ( root . data % 2 == 0 and root . right == None and root . left == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT return root NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = node ( 1 ) NEW_LINE root . left = node ( 2 ) NEW_LINE root . left . left = node ( 8 ) NEW_LINE root . left . right = node ( 10 ) NEW_LINE root . right = node ( 7 ) NEW_LINE root . right . left = node ( 12 ) NEW_LINE root . right . right = node ( 5 ) NEW_LINE newRoot = pruneTree ( root ) NEW_LINE printLevelOrder ( newRoot ) NEW_LINE DEDENT