A tree is symmetric if its left and right subtrees are mirrors of each other. We use a helper function that takes two nodes and checks if they are mirror images.
- Define
is_mirror(t1, t2):- If both are None, return True.
- If one is None or values differ, return False.
- Return
is_mirror(t1.left, t2.right) and is_mirror(t1.right, t2.left).
- Return
is_mirror(root.left, root.right).
- Time Complexity: O(N).
- Space Complexity: O(H).
def is_symmetric(root):
if not root:
return True
def is_mirror(node1, node2):
if not node1 and not node2:
return True
if not node1 or not node2 or node1.val != node2.val:
return False
return is_mirror(node1.left, node2.right) and is_mirror(node1.right, node2.left)
return is_mirror(root.left, root.right)