Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check whether the tree is symmetric or not using recursion in C#?
A symmetric binary tree is a tree where the left subtree is a mirror image of the right subtree. In C#, we can check if a tree is symmetric using recursion by comparing corresponding nodes from left and right subtrees.
The recursive approach works by first checking if the tree is null (which is symmetric by definition), then calling a helper method to compare the left and right subtrees as mirror images of each other.
Algorithm Steps
If the root is null, the tree is symmetric
Compare left subtree with right subtree using a mirror comparison
For mirror comparison: check if both nodes are null (symmetric), if one is null (not symmetric), if values differ (not symmetric)
Recursively compare left child of first node with right child of second node, and right child of first node with left child of second node
Example
using System;
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 IsSymmetricRecursive(Node node) {
if (node == null) {
return true;
}
return IsSymmetricMirror(node.LeftChild, node.RightChild);
}
private bool IsSymmetricMirror(Node node1, Node node2) {
if (node1 == null && node2 == null) {
return true;
}
if (node1 == null || node2 == null) {
return false;
}
if (node1.Value != node2.Value) {
return false;
}
return IsSymmetricMirror(node1.LeftChild, node2.RightChild) &&
IsSymmetricMirror(node1.RightChild, node2.LeftChild);
}
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(" 2 2");
Console.WriteLine(" 3 4 4 3");
Console.WriteLine("Is Symmetric: " + tree.IsSymmetricRecursive(root));
// Create non-symmetric tree
Node root2 = new Node(1);
root2.LeftChild = new Node(2);
root2.RightChild = new Node(2);
root2.LeftChild.LeftChild = new Node(3);
root2.LeftChild.RightChild = new Node(4);
root2.RightChild.LeftChild = new Node(5);
root2.RightChild.RightChild = new Node(3);
Console.WriteLine("\nSecond tree structure:");
Console.WriteLine(" 1");
Console.WriteLine(" 2 2");
Console.WriteLine(" 3 4 5 3");
Console.WriteLine("Is Symmetric: " + tree.IsSymmetricRecursive(root2));
}
}
The output of the above code is −
Tree structure:
1
2 2
3 4 4 3
Is Symmetric: True
Second tree structure:
1
2 2
3 4 5 3
Is Symmetric: False
How It Works
The recursive solution uses two methods:
IsSymmetricRecursive: Main method that handles the null tree case and initiates the mirror comparison
IsSymmetricMirror: Helper method that compares two nodes as mirror images by checking values and recursively comparing left-right and right-left children
The key insight is that for a tree to be symmetric, the left subtree must be a mirror of the right subtree, which means comparing corresponding positions across the center line.
Time and Space Complexity
| Complexity | Value | Explanation |
|---|---|---|
| Time Complexity | O(n) | Visit each node once in the worst case |
| Space Complexity | O(h) | Recursion stack depth equals tree height h |
Conclusion
Checking tree symmetry using recursion in C# involves comparing left and right subtrees as mirror images. The algorithm efficiently determines symmetry by recursively validating that corresponding nodes have equal values and mirror structure, with O(n) time complexity.
