-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path652.cpp
More file actions
51 lines (43 loc) · 1007 Bytes
/
Copy path652.cpp
File metadata and controls
51 lines (43 loc) · 1007 Bytes
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
#include<vector>
#include<string>
#include<map>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
//返回个先遍历
//注意空叶子也要算在内,不然会混淆
string oper(TreeNode* t)
{
if(!t)return string("x");
string ret;
ret.push_back( (char)(t->val) );
ret += oper(t->left);
ret += oper(t->right);
if( ms.find(ret) != ms.end() )
{
if(!ms[ret])
{
ms[ret] = true;
result.push_back(t);
}
}
else
{
ms[ret] = false;
}
return ret;
}
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
if(!root)return result;
oper(root);
return result;
}
map<string, bool> ms;
vector<TreeNode*> result;
};