2Brute2Best
πŸ”₯0

Symmetric Tree

EasyΒ·LeetCode #101 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given the root of a binary tree. Decide whether the tree is a mirror of itself: if you flip it left to right around its center, it looks exactly the same.

Return true if the tree is symmetric, otherwise false.

Input

The root of a binary tree (it has at least one node).

Output

true if the tree is symmetric, false otherwise.

Example 1
Input: root = [1, 2, 2, 3, 4, 4, 3]
Output: true

The left subtree (2 with children 3 and 4) and the right subtree (2 with children 4 and 3) are mirror images of each other.

Example 2
Input: root = [1, 2, 2, null, 3, null, 3]
Output: false

Both 3s are right children. A mirror image would put one of them on the left, so the two sides have different shapes.

Constraints
  • β€’ The number of nodes in the tree is in the range [1, 1000].
  • β€’ βˆ’100 ≀ Node.val ≀ 100
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œmirror of itself”
β†’ Compare the left side with the right side, crossed: the left of one with the right of the other.
β€œsymmetric around its center”
β†’ Both the outer pair and the inner pair must mirror. Each side on its own does not decide the answer.
β€œone root, two children”
β†’ The check starts at the root's two children, which form the first mirror pair.
03

Approach Complexity Ladder

Brute Force β€” build a mirror copy and compareO(N)O(N)

Make an inverted copy of the whole tree, check it equals the original, then free the copy

Optimal β€” compare the two sides as mirror pairsO(N)O(H)

Walk the left and right subtrees together, with the children crossed

Brute Force Intuition

Build a second tree that is the mirror image of the first: at every node, swap the left and right children. A tree is symmetric exactly when it equals its own mirror image, so compare the two with the Same Tree check. The copy costs one new node for every node of the tree.

Recognition rule for this sub-pattern:

Visit nodes in pre/in/post order or walk two trees together node by node.
Remember this Β· Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.