forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain3.cpp
68 lines (51 loc) · 1.42 KB
/
main3.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/// Source : https://leetcode.com/problems/find-duplicate-subtrees/description/
/// Author : liuyubobobo
/// Time : 2018-10-05
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
/// Serializae Binary Tree
/// Using global id key to mark each subtree
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
private:
int id = 1;
public:
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
id = 1;
vector<TreeNode*> res;
unordered_map<string, int> map;
unordered_map<int, int> freq;
dfs(root, map, freq, res);
return res;
}
private:
int dfs(TreeNode* root, unordered_map<string, int>& map,
unordered_map<int, int>& freq, vector<TreeNode*>& res){
if(!root)
return 0;
int lid = dfs(root->left, map, freq, res);
int rid = dfs(root->right, map, freq, res);
string hashcode = to_string(lid) + "#" + to_string(root->val) + "#" + to_string(rid);
if(!map.count(hashcode))
map[hashcode] = id ++;
int rootID = map[hashcode];
if(freq[rootID] == 1)
res.push_back(root);
freq[rootID] ++;
return rootID;
}
};
int main() {
return 0;
}