-
Notifications
You must be signed in to change notification settings - Fork 0
/
Symmetric tree.cpp
46 lines (44 loc) · 1.08 KB
/
Symmetric tree.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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> SRDret,DRSret;
void SRD(TreeNode *root){
if(root == NULL)
return;
SRD(root->left);
SRDret.push_back(root->val);
SRD(root->right);
}
void DRS(TreeNode *root){
if(root == NULL)
return;
DRS(root->right);
DRSret.push_back(root->val);
DRS(root->left);
}
bool Compare(){
int n = SRDret.size();
int m = DRSret.size();
if(n!=m) return false;
for(int i=0;i<m;++i){
if(SRDret[i]!=DRSret[i])
return false;
}
return true;
}
bool isSymmetric(TreeNode *root) {
SRDret.erase(SRDret.begin(),SRDret.end());
DRSret.erase(DRSret.begin(),DRSret.end());
SRD(root);
DRS(root);
return Compare();
}
};