-
Notifications
You must be signed in to change notification settings - Fork 3
/
isSymmetric.cpp
36 lines (36 loc) · 953 Bytes
/
isSymmetric.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
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root, the root of binary tree.
* @return true if it is a mirror of itself, or false.
*/
bool isSymmetric(TreeNode* root) {
return isSymmetricHelp(root, root);
}
bool isSymmetricHelp(TreeNode* root1, TreeNode* root2) {
// Write your code here
if(root1 == NULL && root2 == NULL){
return true;
}
if(root1 == NULL || root2 == NULL){
return false;
}
if(root1->val != root2->val){
return false;
}
return isSymmetricHelp(root1->left, root2->right) &&
isSymmetricHelp(root1->right, root2->left);
}
};