力扣中国101对称二叉树

class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None

# 这道题和第100题很类似,都可以用递归的方法做出来
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
# 若当前节点为空,其左右儿子节点肯定为对称的
if not root :return True
# 写一个递归函数,用来判断
def Tree(p, q):
# 两个节点都为空,则两个节点是对称节点。
if not p and not q :return True
# 两个节点都不为空,而且节点值相同,则为对称节点
if p and q and p.val == q.val:
# 然后继续向下一代比较
return Tree(p.left,q.right) and Tree(p.right,q.left)
return False
return Tree(root.left,root.right)

猜你喜欢

转载自www.cnblogs.com/cong12586/p/12977182.html