Python solution with detailed explanation


  • 0
    G

    Solution

    Symmetric Tree https://leetcode.com/problems/symmetric-tree/?tab=Description

    • Simple recursive solution
    class Solution(object):
        def isSymmetric(self, root):
            if root == None:
                return True
            return self.helper(root.left, root.right)
        
        def helper(self, p, q):
            """
            :type root: TreeNode
            :rtype: bool
            """
            if p == None and q == None:
                return True
            elif p and q and p.val == q.val and self.helper(p.left, q.right) and self.helper(p.right, q.left):
                return True
            else:
                return False
    

Log in to reply
 

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