Skip to content

Commit

Permalink
Time: 27 ms (79.61%), Space: 39 MB (53.64%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Feb 28, 2023
1 parent b3a615a commit e6d6ee1
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 0652-find-duplicate-subtrees/0652-find-duplicate-subtrees.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:
unordered_map<string,int> s;
vector<TreeNode*> v;
string fun(TreeNode* root)
{
string r1="",r2="";
if(root->right!=NULL)
{
r2=r2+fun(root->right);
}
if(root->left!=NULL)
{
r1=r1+fun(root->left);
}
string r=to_string(root->val)+","+r1+","+r2;
if(s.find(r)!=s.end())
{
if(s[r]==1)
{
v.push_back(root);
}
s[r]++;
}
else
{
s[r]=1;
}
return r;
}
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
string k=fun(root);
return v;
}
};

0 comments on commit e6d6ee1

Please sign in to comment.