My python recursive solution.


  • 0
    T
    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        # @param {TreeNode} root
        # @return {boolean}
        def isSymmetric(self, root):
            def checkSym(fst, snd):
                if fst is None and snd is None:
                    return True
                if fst is None or snd is None:
                    return False
                if fst.val != snd.val:
                    return False
                return checkSym(fst.left, snd.right) and checkSym(fst.right, snd.left)
            if root is None:
                return True
            return checkSym(root.left, root.right)

Log in to reply
 

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