Check if a Binary Tree ( not BST ) has duplicate values | Helper function that allocates a new node with the given data and None left and right poers . ; If tree is empty , there are no duplicates . ; If current node 's data is already present.  ; Insert current node ; Recursively check in left and right subtrees . ; To check if tree has duplicates ; Driver Code
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 checkDupUtil ( root , s ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if root . data in s : NEW_LINE INDENT return True NEW_LINE DEDENT s . add ( root . data ) NEW_LINE return checkDupUtil ( root . left , s ) or checkDupUtil ( root . right , s ) NEW_LINE DEDENT def checkDup ( root ) : NEW_LINE INDENT s = set ( ) NEW_LINE return checkDupUtil ( root , s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 2 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE if ( checkDup ( root ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT