# leetcode101题 题解 翻译 C语言版 Python版

101. Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
/ \
2   2
/ \ / \
3  4 4  3


But the following is not:

    1
/ \
2   2
\   \
3    3


Note:
Bonus points if you could solve it both recursively and iteratively.

101.对称树

    1
/ \
2   2
/ \ / \
3  4 4  3

    1
/ \
2   2
\   \
3    3

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     struct TreeNode *left;
*     struct TreeNode *right;
* };
*/

bool isSymmetriclr(struct TreeNode* left, struct TreeNode* right){
if (left == NULL && right == NULL) return true;
if (left == NULL && right != NULL) return false;
if (left != NULL && right == NULL) return false;
if (left->val != right->val) return false;
if (!isSymmetriclr(left->left, right->right)) return false;
if (!isSymmetriclr(left->right, right->left)) return false;
return true;
}

bool isSymmetric(struct TreeNode* root) {
if (root == NULL) return true;
return isSymmetriclr(root->left, root->right);
}

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root: return True
return self.isSymmetriclr(root.left, root.right)

def isSymmetriclr(self, left, right):
if not (left or right): return True
if (left and right==None) or (left==None and right):
return False
if left.val != right.val: return False
if not self.isSymmetriclr(left.left, right.right): return False
if not self.isSymmetriclr(left.right, right.left): return False
return True

11-05
05-24 655
02-11 421
07-04 732
05-31 496
02-13 126
05-09 20
05-23 2301
09-10 108