forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101.cpp
More file actions
46 lines (43 loc) · 1.12 KB
/
101.cpp
File metadata and controls
46 lines (43 loc) · 1.12 KB
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
//method1:
class Solution {
public:
void preOrder(TreeNode* node,vector<int> &v){
if(node){
v.push_back(node->val);
preOrder(node->left,v);
preOrder(node->right,v);
}else{
v.push_back(0);
}
}
void postOrder(TreeNode* node,vector<int> &v){
if(node){
postOrder(node->left,v);
postOrder(node->right,v);
v.push_back(node->val);
}else{
v.push_back(0);
}
}
bool isSymmetric(TreeNode* root) {
vector<int> v1,v2;
if(!root) return true;
preOrder(root->left,v1);
postOrder(root->right,v2);
reverse(v2.begin(),v2.end());
return v1==v2;
}
};
//method2:
class Solution {
public:
bool isSym(TreeNode* left,TreeNode* right){
if(!left || !right) return left==right;
if(left->val!=right->val) return false;
return isSym(left->left,right->right) && isSym(left->right,right->left);
}
bool isSymmetric(TreeNode* root) {
if(!root) return true;
return isSym(root->left,root->right);
}
};