Easy
判断一棵二叉树是否对称
Example, 二叉树[1,2,2,3,4,4,3] 对称:
1
/
2 2
/ \ /
3 4 4 3
二叉树[1,2,2,null,3,null,3]不对称
1
/
2 2
\
3 3
方法与相同树类似,相同树是左左比较,右右比较。这里复制自己后,左右比较,右左比较即可。
# 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
"""
return self.helper(root, root)
def helper(self, x, y):
if x and y:
if x.val == y.val:
return self.helper(x.left, y.right) and self.helper(x.right,y.left)
else:
return False
elif x == y:
return True
else:
return False