๐ŸŒฒ Symmetric Tree (LeetCode 101) โ€” Recursive and Iterative Solutions Explained

CHAO DUCHAO DU
3 min read

Checking whether a binary tree is symmetric is a classic recursion problem โ€” and a great way to learn how to traverse a tree in mirrored fashion. In this post, we'll cover both the recursive and iterative solutions to LeetCode 101 with clear explanations, diagrams, and code.

๐Ÿ“Œ Problem Statement

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

โœ… Examples

Input:  [1, 2, 2, 3, 4, 4, 3]
Output: true

Input:  [1, 2, 2, null, 3, null, 3]
Output: false

๐Ÿง  Core Concept

A binary tree is symmetric if the left and right subtrees are mirror images of each other.

That means:

  • left.left == right.right

  • left.right == right.left


๐Ÿ” Recursive Solution (Top-Down Mirror Check)

๐Ÿ’ก Idea

We define a helper function compare(left, right) that checks whether two subtrees are mirror images.

โœ… Java Code

class Solution {
    public boolean isSymmetric(TreeNode root) {
        return compare(root.left, root.right);
    }

    private boolean compare(TreeNode left, TreeNode right) {
        if (left == null && right == null) return true;
        if (left == null || right == null) return false;
        if (left.val != right.val) return false;

        return compare(left.left, right.right) && compare(left.right, right.left);
    }
}

๐Ÿ” Breakdown

At each node, we check:

  1. If both nodes are null: โœ”๏ธ symmetric

  2. If only one is null: โŒ not symmetric

  3. If values differ: โŒ not symmetric

  4. Recursively check:

    • Outer pair: left.left vs right.right

    • Inner pair: left.right vs right.left


๐Ÿ”„ Iterative Solution Using Deque

๐Ÿ’ก Idea

We simulate the comparison of mirrored nodes using a double-ended queue (Deque):

  • Enqueue left to the front

  • Enqueue right to the back

  • Pop them and compare, then enqueue their child nodes in mirrored order

โœ… Java Code

class Solution {
    public boolean isSymmetric(TreeNode root) {
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offerFirst(root.left);
        deque.offerLast(root.right);

        while (!deque.isEmpty()) {
            TreeNode left = deque.pollFirst();
            TreeNode right = deque.pollLast();

            if (left == null && right == null) continue;
            if (left == null || right == null || left.val != right.val) return false;

            // Mirror order insert
            deque.addFirst(left.left);
            deque.addFirst(left.right);
            deque.addLast(right.right);
            deque.addLast(right.left);
        }

        return true;
    }
}

๐Ÿ” Visual

        1
      /   \
     2     2
    / \   / \
   3  4  4   3

Check:

  • (2, 2)

  • (3, 3) and (4, 4)

  • Each level mirrors the other


๐Ÿง  Time and Space Complexity

MetricRecursiveIterative
TimeO(n)O(n)
SpaceO(h) stack (recursion)O(n) queue
  • n = number of nodes

  • h = height of tree


โœ… Summary

TopicNotes
Problem TypeBinary Tree, Recursion, BFS, Mirror
DifficultyEasy (but very interview-friendly)
Key SkillsRecursive thinking, pointer comparison
Interview BonusAlso tests your ability to write clean, readable logic

๐Ÿง˜ Takeaways

  • Symmetric trees = mirrored traversal

  • Recursive and iterative solutions follow the same mirror pattern logic

  • Always check null before accessing .val to avoid NPE


โœ๏ธ My Practice Notes

  • Solved using both approaches

  • Time taken: ~10 mins

  • Could easily explain this in a real interview


  • 100. Same Tree

  • 226. Invert Binary Tree

  • 110. Balanced Binary Tree

0
Subscribe to my newsletter

Read articles from CHAO DU directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

CHAO DU
CHAO DU