Loading

Symmetric Tree

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

class Solution(object):
    def isSymmetric(self, root):
        if not root:
			return True
        def dfs(left,right):
			# 递归的终止条件是两个节点都为空
			# 或者两个节点中有一个为空
			# 或者两个节点的值不相等
			if not (left or right):
				return True
			if not (left and right):
				return False
			if left.val!=right.val:
				return False
			return dfs(left.left,right.right) and dfs(left.right,right.left)
		# 用递归函数,比较左节点,右节点
        return dfs(root.left,root.right)
posted @ 2023-06-29 10:41  Artwalker  阅读(12)  评论(0编辑  收藏  举报
Live2D