To check if tree has duplicates
public static boolean checkDup ( Node root ) { HashSet < Integer > s = new HashSet < > ( ) ; return checkDupUtil ( root , s ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 2 ) ; root . left . left = new Node ( 3 ) ; if ( checkDup ( root ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }