101. Symmetric Tree
easyDetermine whether a binary tree is a mirror image of itself around its center. Tests two-tree recursion under a structural-mirror predicate.
By Sam K., Founder, InterviewChamp.AI · Last verified
Problem
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Constraints
The number of nodes in the tree is in the range [1, 1000].-100 <= Node.val <= 100
Examples
Example 1
root = [1,2,2,3,4,4,3]trueExample 2
root = [1,2,2,null,3,null,3]falseSolve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Hints
Progressive — try the first before opening the next.
Hint 1
Compare left subtree with right subtree treated as a 'mirror'.
Hint 2
Helper isMirror(a, b): both null = true; one null = false; values differ = false.
Hint 3
Then isMirror(a.left, b.right) AND isMirror(a.right, b.left) — note the CROSS pairing.
Solution approach
Reveal approach
Two-tree recursion with cross-pairing. Helper isMirror(a, b): if both null, return true; if exactly one is null, return false; if a.val != b.val, return false. Otherwise return isMirror(a.left, b.right) AND isMirror(a.right, b.left) — the crucial detail is the cross: a's left mirrors b's right and vice versa. Top-level call: isMirror(root.left, root.right) (or true if root is null). O(n) time, O(h) recursion space. Iterative variant: BFS queue with pairs (a, b) processed in mirror order.
Complexity
- Time
- O(n)
- Space
- O(h)
Related patterns
- dfs
- recursion
Related problems
- 100. Same Tree
- 226. Invert Binary Tree
Asked at
Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).
- Amazon
- Microsoft
- Bloomberg
More Trees practice problems
- 94. Binary Tree Inorder Traversal
- 98. Validate Binary Search Tree
- 100. Same Tree
- 102. Binary Tree Level Order Traversal
- 103. Binary Tree Zigzag Level Order Traversal
- 104. Maximum Depth of Binary Tree
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 108. Convert Sorted Array to Binary Search Tree