Skip to content

101 symmetric tree

101. Symmetric Tree

题目:

https://leetcode.com/problems/symmetric-tree/

难度:

Easy

两棵树symmetric, 有几种可能:

  • 均为none ,symmetric
  • 左孩子,右孩子都不存在,并且值相等, symmetric
  • 右子树 和 另一棵树的左子树相等,左子树 和另一颗树的右子树相等 🌲
class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if not root:
            return True
        return self.symmetric(root.left, root.right)

    def symmetric(self, l1, l2):
        if not l1 or not l2:
            if not l1 and not l2:
                return True
            else:
                return False
        if l1.val == l2.val:
            return self.symmetric(l1.left, l2.right) and self.symmetric(l1.right, l2.left)
        else:
            return False


回到顶部