classSolution{Map<String,Integer> map;List<TreeNode> res;publicList<TreeNode>findDuplicateSubtrees(TreeNode root){
map =newHashMap();
res =newArrayList();preorder(root);return res;}privateStringpreorder(TreeNode node){if(node ==null)return"null";// Assemble current treeString s =String.join(",",String.valueOf(node.val),preorder(node.left),preorder(node.right));// Found duplicateif(map.get(s)!=null&& map.get(s)==1){
res.add(node);}// Add to the map
map.put(s, map.getOrDefault(s,0)+1);return s;}}