Map<String, Integer> map = new HashMap<>();
List<TreeNode> ans = new ArrayList<>();
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
dfs(root);
return ans;
}
String dfs(TreeNode root) {
//将该树序列化存储在哈希表,判断是否出现重复
if (root == null) return " ";
StringBuilder sb = new StringBuilder();
sb.append(root.val).append("-");
sb.append(dfs(root.left)).append(dfs(root.right));
String key = sb.toString();
map.put(key, map.getOrDefault(key, 0) + 1);
if (map.get(key) == 2) ans.add(root);
return key;
}