Given the root
of a binary tree, determine if it is a mirror of itself (symmetric around its center). For example:
Example 1:
Consider the following binary tree:
1
/ \
2 2
/ \ / \
3 4 4 3
This tree is symmetric because the left and right subtrees are mirror images of each other. Therefore, the output should be true
.
Example 2:
Consider the following binary tree:
1
/ \
2 2
\ \
3 3
This tree is not symmetric because the left and right subtrees are not mirror images of each other. Specifically, the right child of the left subtree is 3, while the left child of the right subtree is null. Therefore, the output should be false
.
Write a function that takes the root of a binary tree as input and returns true
if the tree is symmetric and false
otherwise.
Things to consider:
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to determine if a tree is symmetric involves checking all possible arrangements of the tree's structure to see if any of them form a mirrored image of itself. We essentially explore every possible combination to exhaustively test for symmetry. It is like meticulously comparing each node's position and value with its potential mirror counterpart in the entire tree.
Here's how the algorithm would work step-by-step:
def is_symmetric_brute_force(root):
# Handle the base case where the tree is empty
if not root:
return True
# This helper function exhaustively checks all possible mirror images
def are_nodes_symmetric(left_node, right_node):
if not left_node and not right_node:
return True
if not left_node or not right_node:
return False
# If values are unequal, subtree cannot be symmetric
if left_node.val != right_node.val:
return False
# Recursively check the mirroring subtrees
return are_nodes_symmetric(left_node.left, right_node.right) and \
are_nodes_symmetric(left_node.right, right_node.left)
return are_nodes_symmetric(root.left, root.right)
The problem asks us to check if a tree is symmetric, meaning it's a mirror image of itself. The most efficient way to do this is to compare the left and right subtrees simultaneously to see if they match up perfectly.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def is_symmetric(root: TreeNode) -> bool:
def are_mirror_images(left_node: TreeNode, right_node: TreeNode) -> bool:
# If both are null, they are a mirror
if not left_node and not right_node:
return True
# Only one is null, so not a mirror
if not left_node or not right_node:
return False
# Values don't match, not a mirror
if left_node.val != right_node.val:
return False
# Recursively check the subtrees
return (are_mirror_images(left_node.left, right_node.right) and\
are_mirror_images(left_node.right, right_node.left))
# The tree is symmetric if the root's
# left and right subtrees are mirrors.
if not root:
return True
return are_mirror_images(root.left, root.right)
Case | How to Handle |
---|---|
Null root | Return true since a null tree is considered symmetric. |
Single node tree | Return true as a single node tree is inherently symmetric. |
Unbalanced tree where one side is deeper than the other | The recursive/iterative check will eventually encounter a null node on the shorter side and compare it to a non-null node on the deeper side, resulting in false. |
Large tree depth, potential stack overflow with recursion | Consider using an iterative approach (e.g., using a queue) to avoid stack overflow errors, especially in languages with limited stack space. |
Tree with identical values on all nodes, but asymmetric structure | The algorithm should compare structure as well as values; if the structure is different, even with identical values, it returns false. |
One subtree is null, the other is not | Comparison of a null node with a non-null node will return false indicating asymmetry. |
Integer overflow during comparison (if values can be very large) | The problem description usually assumes standard integers; if large numbers are expected, ensure the comparison method is robust to potential overflows. |
Skewed tree (all nodes on one side) | A skewed tree is clearly not symmetric, and the algorithm should correctly identify this case and return false. |