lc101.对称二叉树

 

题目链接

这个算法的时间复杂度是O(N),空间复杂度是O(N)。

# 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:
        if not root:
            return True
        return self.recur(root.left, root.right)

    def recur(self, L, R):
        if not L and not R:
            return True
        if not L or not R or L.val != R.val:
            return False
        return self.recur(L.right, R.left) and self.recur(R.right, L.left)