题目链接:https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/
难度:简单

描述:
请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。

题解

  1. # Definition for a binary tree node.
  2. # class TreeNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7. class Solution:
  8. def isSymmetric(self, root: TreeNode) -> bool:
  9. def recursion(l, r):
  10. if l is None and r is None:
  11. return True
  12. if l is None or r is None or l.val != r.val:
  13. return False
  14. return recursion(l.left, r.right) and recursion(l.right, r.left)
  15. if root is None:
  16. return True
  17. return recursion(root.left, root.right)