class Solution {
public:
string transfer(unordered_map<TreeNode*,string>&hash,TreeNode* root)
{
if(!root) return "#";
string l,r;
l=transfer(hash, root->left);
r=transfer(hash, root->right);
hash[root]=to_string(root->val)+l+r;
return to_string(root->val)+l+r;
}
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
vector<TreeNode*> res;
if(!root) return res;
unordered_map<TreeNode*, string> hash;
unordered_map<string, TreeNode*> HASH;
unordered_map<string, int> count;
transfer(hash, root);
for(auto n:hash){
HASH[n.second]=n.first;
count[n.second]++;
}
for(auto n:count)
{
if(n.second>1)
res.push_back(HASH[n.first]);
}
return res;
}
};