Delete leaf nodes with value as x | A utility class to allocate a new node ; deleteleaves ( ) ; inorder ( ) ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def deleteLeaves ( root , x ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT root . left = deleteLeaves ( root . left , x ) NEW_LINE root . right = deleteLeaves ( root . right , x ) NEW_LINE if ( root . data == x and root . left == None and root . right == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT return root NEW_LINE DEDENT def inorder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT inorder ( root . left ) NEW_LINE print ( root . data , end = " ▁ " ) NEW_LINE inorder ( root . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 3 ) NEW_LINE root . right = newNode ( 10 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 1 ) NEW_LINE root . right . right = newNode ( 3 ) NEW_LINE root . right . right . left = newNode ( 3 ) NEW_LINE root . right . right . right = newNode ( 3 ) NEW_LINE deleteLeaves ( root , 3 ) NEW_LINE print ( " Inorder ▁ traversal ▁ after ▁ deletion ▁ : ▁ " ) NEW_LINE inorder ( root ) NEW_LINE DEDENT