3 lines clean Python

  • 0

    The key idea is we need to check if L.val == R.val only when L and R are not None.

    class Solution(object):
        def isSymmetric(self, root):
            :type root: TreeNode
            :rtype: bool
            def helper(L, R):
                return L.val == R.val and helper(L.left, R.right) and \
                       helper(L.right, R.left) if L and R else L == R == None
            return helper(root, root)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.