Skip to content

Latest commit

 

History

History

101-SymmetricTree

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Symmetric Tree

Problem can be found in here!

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

Solution: Recursion

class Solution:
    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        return self.is_mirror(root, root)

    def is_mirror(self, left: Optional[TreeNode], right: Optional[TreeNode]) -> bool:
        if not left and not right:
            return True
        elif not left or not right:
            return False

        return left.val == right.val and self.is_mirror(left.right, right.left) and self.is_mirror(left.left, right.right)

Time Complexity: O(n), Space Complexity: O(n)