Symmetric Tree Visualizer & Step-by-Step Algorithm Solution

Check whether a binary tree is a mirror of itself (symmetric around its center) using simultaneous dual-pointer DFS recursion.

Category: trees | Difficulty: Easy

Tags: DFS, Binary Tree, Recursion, Mirror, Symmetry

Symmetric Tree

1
2
3
4
2
4
3
100%
state
resultcomparing
Initialization
1/22
Explanation

Start isSymmetric on binary tree with root: Node(1).

Source Code
1function isSymmetric(root) {
2 if (!root) return true;
3 function isMirror(t1, t2) {
4 if (!t1 && !t2) return true;
5 if (!t1 || !t2) return false;
6 if (t1.val !== t2.val) return false;
7 return isMirror(t1.left, t2.right) && isMirror(t1.right, t2.left);
8 }
9 return isMirror(root.left, root.right);
10}