-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymmetric Tree 2
33 lines (32 loc) · 913 Bytes
/
Symmetric Tree 2
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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root == NULL) return true;
return isSym(root->left, root->right);
}
bool isSym(TreeNode *left, TreeNode *right){
if(left == NULL)
return right == NULL;
if(right == NULL)
return left == NULL;
if(left->val != right->val)
return false;
if(!isSym(left->left, right->right))
return false;
if(!isSym(left->right, right->left))
return false;
return true;
}
};
//FROM http://fisherlei.blogspot.com/2013/01/leetcode-symmetric-tree.html