-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion28.cpp
More file actions
36 lines (32 loc) · 875 Bytes
/
Copy pathquestion28.cpp
File metadata and controls
36 lines (32 loc) · 875 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
/*
请实现一个函数,用来判断一颗二叉树是不是对称的。
如果一颗二叉树和它的镜像一样,那么他是对称的,
Xiaobin Tian;
*/
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(nullptr), right(nullptr) {
}
};
class Solution {
public:
bool isSymmetrical(TreeNode* p, TreeNode* q){
if(p != nullptr && q != nullptr){
if(p->val != q->val)
return false;
else
return isSymmetrical(p->left, q->right) && isSymmetrical(p->right, q->left);
}
else if(p == nullptr && q == nullptr)
return true;
else
return false;
}
bool isSymmetrical(TreeNode* pRoot){
return isSymmetrical(pRoot, pRoot);
}
};