题目

面试题28. 对称的二叉树

请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
    1
   / \
  2   2
 / \ / \
3  4 4  3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
    1
   / \
  2   2
   \   \
   3    3

 

分析

 
 
【剑指offer】面试题28. 对称的二叉树

 

 

解题

def isSymmetric(self, root: TreeNode) -> bool:
        def recur(L, R):
            if not L and not R: return True
            if not L or not R or L.val != R.val: return False
            return recur(L.left, R.right) and recur(L.right, R.left)

        return recur(root.left, root.right) if root else True

 

 
 

相关文章:

  • 2021-12-01
  • 2021-08-08
  • 2021-06-13
  • 2022-01-27
  • 2021-05-30
  • 2022-01-09
  • 2022-12-23
猜你喜欢
  • 2021-12-25
  • 2021-05-31
  • 2021-10-16
  • 2021-05-24
相关资源
相似解决方案