How to check whether the tree is symmetric or not using iterative in C#?

A symmetric binary tree is one where the left subtree is a mirror reflection of the right subtree. In the iterative approach, we use two queues to traverse both subtrees simultaneously and compare their values in mirror positions.

The algorithm works by enqueueing nodes from the left and right subtrees in opposite orders. For the left subtree, we add left child first, then right child. For the right subtree, we add right child first, then left child. This ensures we compare mirror positions correctly.

Syntax

Following is the basic structure for checking tree symmetry iteratively −

public bool IsSymmetric(Node root) {
   if (root == null) return true;
   
   Queue<Node> leftQueue = new Queue<Node>();
   Queue<Node> rightQueue = new Queue<Node>();
   
   leftQueue.Enqueue(root.LeftChild);
   rightQueue.Enqueue(root.RightChild);
   
   while (leftQueue.Count > 0 && rightQueue.Count > 0) {
      // Compare and enqueue children
   }
   
   return true;
}

Symmetric Binary Tree Structure 1 2 2 3 4 4 3 Mirror Line Left subtree mirrors right subtree

How It Works

The algorithm uses two queues to simultaneously traverse the left and right subtrees. At each step, it compares nodes that should be mirror reflections of each other. If any pair doesn't match or one subtree has more nodes than the other, the tree is not symmetric.

Example

using System;
using System.Collections.Generic;

public class TreesPgm {
   public class Node {
      public int Value;
      public Node LeftChild;
      public Node RightChild;
      
      public Node(int value) {
         this.Value = value;
      }
      
      public override String ToString() {
         return "Node=" + Value;
      }
   }
   
   public bool IsSymmetricIterative(Node node) {
      if (node == null) {
         return true;
      }
      
      Queue<Node> Q1 = new Queue<Node>();
      Queue<Node> Q2 = new Queue<Node>();
      Q1.Enqueue(node.LeftChild);
      Q2.Enqueue(node.RightChild);
      
      while (Q1.Count > 0 && Q2.Count > 0) {
         Node n1 = Q1.Dequeue();
         Node n2 = Q2.Dequeue();
         
         if ((n1 == null && n2 != null) || (n1 != null && n2 == null)) {
            return false;
         }
         
         if (n1 != null) {
            if (n1.Value != n2.Value) {
               return false;
            }
            Q1.Enqueue(n1.LeftChild);
            Q1.Enqueue(n1.RightChild);
            Q2.Enqueue(n2.RightChild);
            Q2.Enqueue(n2.LeftChild);
         }
      }
      return true;
   }
   
   public static void Main() {
      TreesPgm tree = new TreesPgm();
      
      // Create symmetric tree: 1, 2, 2, 3, 4, 4, 3
      Node root = new Node(1);
      root.LeftChild = new Node(2);
      root.RightChild = new Node(2);
      root.LeftChild.LeftChild = new Node(3);
      root.LeftChild.RightChild = new Node(4);
      root.RightChild.LeftChild = new Node(4);
      root.RightChild.RightChild = new Node(3);
      
      Console.WriteLine("Tree structure:");
      Console.WriteLine("       1");
      Console.WriteLine("      / ");
      Console.WriteLine("     2   2");
      Console.WriteLine("    / \ / ");
      Console.WriteLine("   3  4 4  3");
      Console.WriteLine();
      Console.WriteLine("Is symmetric: " + tree.IsSymmetricIterative(root));
      
      // Test asymmetric tree
      Node asymmetricRoot = new Node(1);
      asymmetricRoot.LeftChild = new Node(2);
      asymmetricRoot.RightChild = new Node(2);
      asymmetricRoot.LeftChild.LeftChild = new Node(3);
      asymmetricRoot.RightChild.LeftChild = new Node(3);
      
      Console.WriteLine("Asymmetric tree result: " + tree.IsSymmetricIterative(asymmetricRoot));
   }
}

The output of the above code is −

Tree structure:
       1
      / \
     2   2
    / \ / \
   3  4 4  3

Is symmetric: True
Asymmetric tree result: False

Key Rules

  • An empty tree is considered symmetric.

  • For each pair of nodes being compared, both must be null or both must have the same value.

  • The left child of the left subtree must match the right child of the right subtree.

  • The right child of the left subtree must match the left child of the right subtree.

Conclusion

The iterative approach to check tree symmetry uses two queues to traverse both subtrees simultaneously in mirror order. This method efficiently determines if a binary tree is symmetric by comparing corresponding nodes at each level, ensuring the tree structure mirrors itself across the vertical axis through the root.

Updated on: 2026-03-17T07:04:36+05:30

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements