652. Find Duplicate Subtrees 發表於 2023-02-13 | 分類於 leetcode problemsolution1234567891011121314151617181920212223242526272829303132333435/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */class Solution {public: unordered_map<string, int> memo; string checkDuplicate(TreeNode * root, vector<TreeNode*> & ret){ if(!root) return "#"; string cur ; cur += checkDuplicate(root->left, ret) + ","; cur += checkDuplicate(root->right, ret) + ","; cur+= to_string(root->val); if(memo.find(cur)!=memo.end() && memo[cur] ==1){ ret.push_back(root); } memo[cur]++; return cur; } vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) { vector<TreeNode*> ret; checkDuplicate(root, ret); return ret; }}; analysis