class Solution {
public:
vector<TreeNode*> res;
unordered_map<string,int> hashmap;//记录每一个子树出现的次数
string dfs(TreeNode* root)
{
if(!root) return "";
string str="";
str+=to_string(root->val)+',';
str+=dfs(root->left)+',';
str+=dfs(root->right)+',';
hashmap[str]++;
if(hashmap[str]==2)//防止同一种子树重复计算
res.push_back(root);
return str;
}
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
dfs(root);
return res;
}
};
标签:子树,return,hashmap,res,dfs,652,str,root,LeetCode
From: https://www.cnblogs.com/tangxibomb/p/17438267.html