Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Example 1:
Input: root = [1,2,2,3,4,4,3] Output: true
Example 2:
Input: root = [1,2,2,null,3,null,3] Output: false
Constraints:
[1, 1000].-100 <= Node.val <= 100When 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:
To check if a tree is symmetric, we can compare its left side with its right side. The brute force approach involves systematically checking if the corresponding parts of the left and right halves mirror each other perfectly.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.value = val
self.left_child = left
self.right_child = right
def is_symmetric(root_node):
if not root_node:
return True
return check_mirror_subtrees(root_node.left_child, root_node.right_child)
def check_mirror_subtrees(left_subtree_node, right_subtree_node):
# If both nodes are null, they are symmetric counterparts
if not left_subtree_node and not right_subtree_node:
return True
# If one node exists and the other doesn't, they are not symmetric
if not left_subtree_node or not right_subtree_node:
return False
# If the values of the current nodes don't match, they are not symmetric
if left_subtree_node.value != right_subtree_node.value:
return False
# Recursively check the outer children and the inner children
return (check_mirror_subtrees(left_subtree_node.left_child, right_subtree_node.right_child) and
check_mirror_subtrees(left_subtree_node.right_child, right_subtree_node.left_child))To check if a tree is symmetric, we need to compare its left side with its right side in a mirrored fashion. The core idea is to simultaneously traverse both halves of the tree, ensuring that corresponding nodes have the same value and are positioned symmetrically.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, value=0, left=None, right=None):
self.value = value
self.left = left
self.right = right
def is_symmetric_tree(root_node):
if not root_node:
return True
def check_mirrored_subtrees(left_subtree_node, right_subtree_node):
# If both nodes are null, they are considered symmetric.
if not left_subtree_node and not right_subtree_node:
return True
# If one node is null but the other isn't, they are not symmetric.
if not left_subtree_node or not right_subtree_node:
return False
# Values must match for symmetric nodes.
if left_subtree_node.value != right_subtree_node.value:
return False
# Recursively check the outer pair (left's left and right's right).
# Then recursively check the inner pair (left's right and right's left).
return (check_mirrored_subtrees(left_subtree_node.left, right_subtree_node.right) and
check_mirrored_subtrees(left_subtree_node.right, right_subtree_node.left))
# Start the mirrored comparison from the children of the root node.
return check_mirrored_subtrees(root_node.left, root_node.right)| Case | How to Handle |
|---|---|
| An empty tree (root is null) | An empty tree is considered symmetric, so the solution should return true immediately. |
| A tree with only a root node | A single node tree is symmetric by definition, and the recursive comparison should handle this base case correctly. |
| A tree with only a left child or only a right child | These are not symmetric and should be identified as false by comparing a non-null node with a null node. |
| A tree with perfect symmetry | The recursive or iterative approach should correctly traverse and compare all mirrored nodes. |
| A tree that is not symmetric at any level | The comparison function should detect the first asymmetry and return false promptly. |
| A tree with identical values at mirrored positions but structurally different | The structural comparison (left child of left subtree vs. right child of right subtree) is crucial and should be handled. |
| Very deep trees that could lead to stack overflow with recursion | An iterative approach using a queue or stack would mitigate potential stack overflow issues for extremely deep trees. |
| Trees with large numbers of nodes | The solution's time complexity should be O(N) and space complexity O(H) (or O(N) in worst case for iterative), which is efficient for large trees. |