Find Duplicate Subtrees Problem
Description
LeetCode Problem 652.
Given the rootof a binary tree, return all duplicate subtrees.
For each kind of duplicate subtrees, you only need to return the root node of any one of them. Two trees are duplicate if they have the same structure with the same node values.
Example 1:
1
2
Input: root = [1,2,3,4,null,2,4,null,null,4]
Output: [[2,4],[4]]
Example 2:
1
2
Input: root = [2,1,1]
Output: [[1]]
Example 3:
1
2
Input: root = [2,2,2,3,null,3,null]
Output: [[2,3],[3]]
Constraints:
- The number of the nodes in the tree will be in the range [1, 10^4]
- -200 <= Node.val <= 200
Sample C++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 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:
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
unordered_map<string, int> m;
vector<TreeNode*> res;
DFS(root, m, res);
return res;
}
string DFS(TreeNode* root, unordered_map<string, int>& m, vector<TreeNode*>& res){
if (!root) return "";
string s = to_string(root->val) + "," + DFS(root->left, m, res) + "," + DFS(root->right, m, res);
if (m[s]++ == 1)
res.push_back(root);
return s;
}
};