Symmetric Tree - Complete Solution Guide
Symmetric Tree is LeetCode problem 101, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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: The number of nodes in the tree is in the range [1, 1000] . -100 <= Node.val <= 100 Follow up: Could you solve it both recursively and iteratively?
Detailed Explanation
This problem asks us to determine if a given binary tree is a mirror image of itself when reflected across its vertical center. It's not just about node values being identical at the same level; the structure and values must be perfectly symmetrical. Imagine drawing a line straight down the middle of the tree – the left side should look exactly like the right side, but flipped.
Solution Approach
The provided solution employs a clever recursive helper function, `isMirror(t1, t2)`, to check if two subtrees (`t1` and `t2`) are mirror images of each other. The core idea is to initiate this check by calling `isMirror(root, root)`. This sets up the problem as comparing the tree against itself for mirroring, where the 'left half' will be traversed using `t1` and the 'right half' using `t2` (conceptually starting from `root.left` and `root.right` respectively in subsequent calls). The `isMirror` function then recursively validates three conditions: 1) if both `t1` and `t2` are null, they are symmetric; 2) if only one is null, they are not symmetric; 3) if both exist, their values must be equal, AND `t1.left` must mirror `t2.right`, AND `t1.right` must mirror `t2.left`. This last step is crucial as it enforces the cross-comparison required for symmetry.
Step-by-Step Algorithm
- Step 1: Check for empty tree: If the root is null, return `true` (an empty tree is symmetric).
- Step 2: Call the recursive helper function `isMirror` with the root's left and right subtrees as arguments.
- Step 3: In `isMirror` function: Check the base cases (both subtrees null, one subtree null, or values different).
- Step 4: Recursively call `isMirror` on mirrored children (left's left to right's right, and left's right to right's left).
- Step 5: Return `true` only if all recursive calls and value checks return `true`, indicating perfect mirroring.
Key Insights
- **Coordinated Symmetric Traversal:** Instead of a single standard tree traversal, this problem demands a coordinated traversal pattern. We're effectively running two 'virtual pointers' (`t1` and `t2`) simultaneously. When `t1` explores a `left` child, `t2` must explore a `right` child to maintain the mirror comparison, and vice versa. This `t1.left` vs `t2.right` and `t1.right` vs `t2.left` logic is the bedrock of symmetric checks.
- **Recursive Definition of Mirroring:** The problem lends itself perfectly to recursion because the definition of a symmetric tree is inherently recursive. A tree is symmetric if its root's children's subtrees are mirror images of each other. The `isMirror` function directly translates this by ensuring the values match at the current level and then recursively confirming the mirrored relationship between the respective children.
- **Precise Null Node Handling:** The base cases `if not t1 and not t2: return True` and `if not t1 or not t2: return False` are more than just exit conditions; they are fundamental to correctly identifying symmetry. Two null branches are trivially symmetric, while a null branch paired with an existing node signifies an immediate break in symmetry. Correctly managing these null scenarios ensures that trees with missing nodes (like `[1,2,2,null,3,3,null]`) are evaluated accurately without crashing or producing false positives.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Tree, Depth-First Search, Breadth-First Search, Binary Tree.
Companies
Asked at: Adobe, Apple, Bloomberg, Comcast, Google, LinkedIn, Meta, Microsoft, Yahoo, Yandex.