def is_Symmetic(root):
'''
Day49:是否为对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1 1 2^0 - 1
/
2 2 1 2^1 - 1
/ \ /
3 4 4 3 2 2^2
/ \ / \ / \ /
5 6 7 8 8 7 6 5
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/
2 2
\
3 3
来源:力扣(LeetCode) https://leetcode-cn.com/problems/symmetric-tree
'''
if not root:
return True
def sub(left, right):
if not left and not right:
return True
if not left or not right :
return False
return left.val == right.val and sub(left.left,right.right) and sub(left.right, right.left)
return sub(root.left, root.right)
网友评论