leetcode_101. 对称二叉树

给定一个二叉树,检查它是否是镜像对称的。

 

例如,二叉树 [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
 

进阶:

你可以运用递归和迭代两种方法解决这个问题吗?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/symmetric-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        def check(t1:TreeNode, t2:TreeNode)->bool:
            if not t1 and not t2 :
                return True
            if not t1 or not t2:
                return False
            return t1.val==t2.val and check(t1.left,t2.right) and check(t1.right,t2.left)

        return check(root,root)
posted @ 2020-11-27 09:58  hqzxwm  阅读(86)  评论(0编辑  收藏  举报