101. 对称二叉树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
# print(root.left.val,root.right.val)
def Symmetric(a,b):
if (a and b and a.val != b.val) or (a and b==None) or (a==None and b) :
return False
if (a==None and b==None):
return True
if (a.val==b.val):
result1 = Symmetric(a.left,b.right)
result2 = Symmetric(a.right,b.left)
if result1 != False and result2 != False:
return True
else:
return False
return Symmetric(root.left,root.right)