Introduction
Symmetric Tree means:
- left subtree
and right subtree - should mirror each other
Conditions:
- structure must match
- mirrored node values must match
Example:
1/ \
2 2
/ \ / \
3 4 4 3
Output:
True
Explanation:
Left subtree is mirror image of right subtree. This problem is one of the most important applications of:
DFS Traversal Constraints
1 <= Number of Nodes <= 10^5 Approach : Recursive DFS Solution
Explanations:
Explanation:
The idea is:
- compare opposite nodes
- recursively validate mirror structure
Steps:
- Compare left node.
- Compare right node.
- Check node values.
- Recurse opposite children.
- Validate mirror structure.
Mirror Conditions:
- both nodes null → valid
- one node null → invalid
- values mismatch → invalid
This approach:
- uses DFS recursion
- validates mirror symmetry
Dry Run
Compare:2 and 2 Values match.
Compare:
3 and 3 Values match.
Compare:
4 and 4
Values match.
All mirrored nodes matched. Tree is symmetric.
Practice :
Complexity Analysis :
Time Complexity:- O(n)
Explanation : Every tree node is visited once.
Space Complexity:- O(h)
Explanation : Recursion stack depends on tree height.
Why This Problem is Important