Symmetric Tree

Last Updated : 4 Oct, 2025

Given the root of a binary tree, determine whether it is symmetric around root, i.e., check if the binary tree is a mirror image of itself.

Example:

Input:

420046791


Output: true
Explanation: Tree is mirror image of itself i.e. tree is symmetric.

Input:

420046790


Output: false
Explanation: Tree is not mirror image of itself i.e. tree is not symmetric.

Try It Yourself
redirect icon

[Approach - 1] Using Recursion

The idea is to recursively compare the left and right subtrees of the root. For the tree to be symmetric, the root values of the left and right subtrees must match, and their corresponding children must also be mirrors.

C++
#include <iostream>
using namespace std;

// Node Structure
class Node {
public:
    int data;
    Node *left, *right;

    Node(int val) {
        data = val;
        left = right = nullptr;
    }
};

// Recursive helper function to
// check if two subtrees are mirror images
bool isMirror(Node* leftSub, Node* rightSub) {
    
    if (leftSub == nullptr && rightSub == nullptr) 
        return true;
    
    // One of them is null, so they aren't mirror images
    if (leftSub == nullptr || rightSub == nullptr || 
        			leftSub->data != rightSub->data) {
        return false;
    }
    
    // Check if the subtrees are mirrors
    return isMirror(leftSub->left, rightSub->right) &&
           isMirror(leftSub->right, rightSub->left);
}

bool isSymmetric(Node* root) {
    
    if (root == nullptr) 
        return true;
    
    return isMirror(root->left, root->right);
}

int main() {
    
    // Creating a sample symmetric binary tree
    //       10
    //       / \
    //      5   5
    //     /     \
    //    2       2
    Node* root = new Node(10);
    root->left = new Node(5);
    root->right = new Node(5);
    root->left->left = new Node(2);
    root->right->right = new Node(2);

    if(isSymmetric(root))
		cout << "true";
  	else 
      	cout << "false";
    
    return 0;
}
Java
// Node Structure
class Node {
    int data;
    Node left, right;

    Node(int val)
    {
        data = val;
        left = right = null;
    }
}

class GFG {

    // Recursive helper function to
    // check if two subtrees are mirror images
    static boolean isMirror(Node leftSub, Node rightSub) {

        if (leftSub == null && rightSub == null)
            return true;

        // One of them is null, so they aren't mirror images
        if (leftSub == null || rightSub == null
            || leftSub.data != rightSub.data)
            return false;

        // Check if the subtrees are mirrors
        return isMirror(leftSub.left, rightSub.right)
            && isMirror(leftSub.right, rightSub.left);
    }

    static boolean isSymmetric(Node root) {
        if (root == null)
            return true;

        return isMirror(root.left, root.right);
    }

    public static void main(String[] args) {

        // Creating a sample symmetric binary tree
        //       10
        //       / \
        //      5   5
        //     /     \
        //    2       2
        Node root = new Node(10);
        root.left = new Node(5);
        root.right = new Node(5);
        root.left.left = new Node(2);
        root.right.right = new Node(2);

        if (isSymmetric(root))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
# Node Structure
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

# Recursive helper function to check 
#if two subtrees are mirror images
def isMirror(leftSub, rightSub):
  
    if leftSub is None and rightSub is None:
        return True

    # One of them is null, so they aren't mirror images
    if leftSub is None or rightSub is None or leftSub.data != rightSub.data:
        return False
	
    # Check if the subtrees are mirrors
    return isMirror(leftSub.left, rightSub.right) and \
           isMirror(leftSub.right, rightSub.left)

def isSymmetric(root):
  
    if root is None:
        return True

    return isMirror(root.left, root.right)

if __name__ == "__main__":
    
    #  Creating a sample symmetric binary tree
    #           10
    #           / \
    #          5   5
    #         /     \
    #        2       2
    root = Node(10)
    root.left = Node(5)
    root.right = Node(5)
    root.left.left = Node(2)
    root.right.right = Node(2)

    print("true" if isSymmetric(root) else "false")
C#
using System;

// Node Structure
class Node {
    public int data;
    public Node left, right;

    public Node(int val) {
        data = val;
        left = right = null;
    }
}

class GFG {

    // Recursive helper function
    // to check if two subtrees are mirror images
    static bool isMirror(Node leftSub, Node rightSub) {
      
        if (leftSub == null && rightSub == null)
            return true;

        // One of them is null, so they aren't mirror images
        if (leftSub == null || rightSub == null || 
            			leftSub.data != rightSub.data)
            return false;

        // Check if the subtrees are mirrors
        return isMirror(leftSub.left, rightSub.right) && 
               isMirror(leftSub.right, rightSub.left);
    }

    static bool isSymmetric(Node root) {
      
        if (root == null)
            return true;

        return isMirror(root.left, root.right);
    }

    static void Main(string[] args) {
       // Creating a sample symmetric binary tree
        //       10
        //       / \
        //      5   5
        //     /     \
        //    2       2
        Node root = new Node(10);
        root.left = new Node(5);
        root.right = new Node(5);
        root.left.left = new Node(2);
        root.right.right = new Node(2);

        Console.WriteLine(isSymmetric(root) ? "true" : "false");
    }
}
JavaScript
// Node Structure
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Recursive helper function to
// check if two subtrees are mirror images
function isMirror(leftSub, rightSub) {

    if (leftSub === null && rightSub === null)
        return true;

    // One of them is null, so they aren't mirror images
    if (leftSub === null || rightSub === null
        || leftSub.data !== rightSub.data)
        return false;

    // Check if the subtrees are mirrors
    return isMirror(leftSub.left, rightSub.right)
           && isMirror(leftSub.right, rightSub.left);
}

function isSymmetric(root) {

    if (root === null)
        return true;

    return isMirror(root.left, root.right);
}

// Driver Code

// Creating a sample symmetric binary tree
//       10
//       / \
//      5   5
//     /     \
//    2       2
let root = new Node(10);
root.left = new Node(5);
root.right = new Node(5);
root.left.left = new Node(2);
root.right.right = new Node(2);

console.log(isSymmetric(root) ? "true" : "false");

Output
true

Time Complexity: O(n)
Auxiliary Space: O(h), where h is height of binary tree due to recursive stack space

[Approach - 2] Using Stack - O(n) Time and O(n) Space

We can use two stacks to check for symmetry of binary tree: one for the left subtree and one for the right. At each step, nodes are popped and compared; if they differ, the tree is not symmetric. Their children are then pushed in mirror order to ensure proper matching.

C++
#include <iostream>
#include <stack>
using namespace std;

// Node Structure
class Node {
public:
    int data;
    Node *left, *right;

    Node(int val) {
        data = val;
        left = right = nullptr;
    }
};

bool isSymmetric(Node* root) {
    if (root == nullptr) 
        return true;

    stack<Node*> s1, s2;

    // Initialize the stacks with the left 
  	// and right subtrees
    s1.push(root->left);
    s2.push(root->right);

    while (!s1.empty() && !s2.empty()) {
        
        // Get the current pair of nodes
        Node* node1 = s1.top();
        Node* node2 = s2.top();
        s1.pop();
        s2.pop();

        // If both nodes are null, continue to the next pair
        if (node1 == nullptr && node2 == nullptr) {
            continue;
        }

        if (node1 == nullptr || node2 == nullptr 
            || node1->data != node2->data) {
            return false;
        }

        // Push children of node1 and node2 in opposite order
        // Push left child of node1 and right child of node2
        s1.push(node1->left);
        s2.push(node2->right);

        // Push right child of node1 and left child of node2
        s1.push(node1->right);
        s2.push(node2->left);
    }

    return s1.empty() && s2.empty();
}

int main() {
    
    // Creating a sample symmetric binary tree
    //       10
    //       / \
    //      5   5
    //     /     \
    //    2       2
    Node* root = new Node(10);
    root->left = new Node(5);
    root->right = new Node(5);
    root->left->left = new Node(2);
    root->right->right = new Node(2);
    if(isSymmetric(root)) 
      	cout << "true";
  	else 
      	cout << "false";

    return 0;
}
Java
import java.util.Stack;

// Node Structure
class Node {
    int data;
    Node left, right;

    Node(int val) {
        data = val;
        left = right = null;
    }
}

class GFG {
  
    static boolean isSymmetric(Node root) {
        if (root == null) {
            return true;
        }

        Stack<Node> s1 = new Stack<>();
        Stack<Node> s2 = new Stack<>();

        // Initialize the stacks with the left
      	// and right subtrees
        s1.push(root.left);
        s2.push(root.right);

        while (!s1.isEmpty() && !s2.isEmpty()) {

            // Get the current pair of nodes
            Node node1 = s1.pop();
            Node node2 = s2.pop();

            // If both nodes are null, continue to the next pair
            if (node1 == null && node2 == null) {
                continue;
            }

            if (node1 == null || node2 == null 
                || node1.data != node2.data) {
                return false;
            }

            // Push children of node1 and node2 in opposite order
            // Push left child of node1 and right child of node2
            s1.push(node1.left);
            s2.push(node2.right);

            // Push right child of node1 and left child of node2
            s1.push(node1.right);
            s2.push(node2.left);
        }

        return s1.isEmpty() && s2.isEmpty();
    }

    public static void main(String[] args) {
      
        // Creating a sample symmetric binary tree
        //       10
        //       / \
        //      5   5
        //     /     \
        //    2       2
        Node root = new Node(10);
        root.left = new Node(5);
        root.right = new Node(5);
        root.left.left = new Node(2);
        root.right.right = new Node(2);

        System.out.println(isSymmetric(root));
    }
}
Python
# Node Structure
class Node:
    def __init__(self, val):
        self.data = val
        self.left = self.right = None

def isSymmetric(root):
    if root is None:
        return True
        
    s1 = []
    s2 = []

    # Initialize the stacks with the 
    # left and right subtrees
    s1.append(root.left)
    s2.append(root.right)

    while s1 and s2:
      
        # Get the current pair of nodes
        node1 = s1.pop()
        node2 = s2.pop()

        # If both nodes are null, continue to the next pair
        if node1 is None and node2 is None:
            continue

        if node1 is None or node2 is None or node1.data != node2.data:
            return False

        # Push children of node1 and node2 in opposite order
        # Push left child of node1 and right child of node2
        s1.append(node1.left)
        s2.append(node2.right)

        # Push right child of node1 and left child of node2
        s1.append(node1.right)
        s2.append(node2.left)

    return len(s1) == 0 and len(s2) == 0

if __name__ == "__main__":
  
    #  Creating a sample symmetric binary tree
    #           10
    #           / \
    #          5   5
    #         /     \
    #        2       2
    root = Node(10)
    root.left = Node(5)
    root.right = Node(5)
    root.left.left = Node(2)
    root.right.right = Node(2)

    print("true" if isSymmetric(root) else "false")
C#
using System;
using System.Collections.Generic;

// Node Structure
class Node {
    public int data;
    public Node left, right;

    public Node(int val) {
        data = val;
        left = right = null;
    }
}

class GFG {
  
    static bool isSymmetric(Node root) {
        if (root == null) {
            return true;
        }

        Stack<Node> s1 = new Stack<Node>();
        Stack<Node> s2 = new Stack<Node>();

        // Initialize the stacks with the left
      	// and right subtrees
        s1.Push(root.left);
        s2.Push(root.right);

        while (s1.Count > 0 && s2.Count > 0) {
          
            // Get the current pair of nodes
            Node node1 = s1.Pop();
            Node node2 = s2.Pop();

            // If both nodes are null, continue to the next pair
            if (node1 == null && node2 == null) {
                continue;
            }

            if (node1 == null || node2 == null 
                || node1.data != node2.data) {
                return false;
            }

            // Push children of node1 and node2 in opposite order
            // Push left child of node1 and right child of node2
            s1.Push(node1.left);
            s2.Push(node2.right);

            // Push right child of node1 and left child of node2
            s1.Push(node1.right);
            s2.Push(node2.left);
        }

        return s1.Count == 0 && s2.Count == 0;
    }

    static void Main(string[] args) {
      
        // Creating a sample symmetric binary tree
        //       10
        //       / \
        //      5   5
        //     /     \
        //    2       2
        Node root = new Node(10);
        root.left = new Node(5);
        root.right = new Node(5);
        root.left.left = new Node(2);
        root.right.right = new Node(2);

        Console.WriteLine(isSymmetric(root) ? "true" : "false");
    }
}
JavaScript
// Node Structure
class Node {
    constructor(val) {
        this.data = val;
        this.left = this.right = null;
    }
}

function isSymmetric(root) {
    if (root === null) {
        return true;
    }

    let s1 = [];
    let s2 = [];

    // Initialize the stacks with the 
    // left and right subtrees
    s1.push(root.left);
    s2.push(root.right);

    while (s1.length > 0 && s2.length > 0) {
    
        // Get the current pair of nodes
        let node1 = s1.pop();
        let node2 = s2.pop();

        // If both nodes are null, continue to the next pair
        if (node1 === null && node2 === null) {
            continue;
        }

        if (node1 === null || node2 === null 
        	|| node1.data !== node2.data) {
            return false;
        }

        // Push children of node1 and node2 in opposite order
        // Push left child of node1 and right child of node2
        s1.push(node1.left);
        s2.push(node2.right);

        // Push right child of node1 and left child of node2
        s1.push(node1.right);
        s2.push(node2.left);
    }

    return s1.length === 0 && s2.length === 0;
}

// Driver Code

// Creating a sample symmetric binary tree
//       10
//       / \
//      5   5
//     /     \
//    2       2
let root = new Node(10);
root.left = new Node(5);
root.right = new Node(5);
root.left.left = new Node(2);
root.right.right = new Node(2);

console.log(isSymmetric(root));

Output
true

[Approach - 3] Using Queue - O(n) Time and O(n) Space

The idea is to check if the left and right subtrees of the root node are mirror images of each other. To do this, we perform a level-order traversal of the binary tree using a queue. Initially, we push the root node into the queue twice. We dequeue two nodes at a time from the front of the queue and check if they are mirror images of each other.

C++
#include <iostream>
#include<queue>
using namespace std;

// Node Structure
class Node {
public:
    int data;
    Node *left, *right;

    Node(int val) {
        data = val;
        left = right = nullptr;
    }
};

bool isSymmetric(Node* root) {
    if (root == nullptr) {
        return true;
    }

    queue<Node*> q;

    // Initialize the queue with the left 
  	// and right subtrees
    q.push(root->left);
    q.push(root->right);

    while (!q.empty()) {
        
        Node* node1 = q.front(); 
        q.pop();
        Node* node2 = q.front();
        q.pop();

        // If both nodes are null, continue to the next pair
        if (node1 == nullptr && node2 == nullptr) {
            continue;
        }

        // If one node is null and the other is not, 
        // or the nodes' data do not match
        // then the tree is not symmetric
        if (node1 == nullptr || node2 == nullptr || 
            node1->data != node2->data) {
            return false;
        }

        // Enqueue children in opposite 
      	// order to compare them
        q.push(node1->left);
        q.push(node2->right);
        q.push(node1->right);
        q.push(node2->left);
    }
    return true;
}

int main() {
    
    // Creating a sample symmetric binary tree
    //       10
    //       / \
    //      5   5
    //     /     \
    //    2       2
    Node* root = new Node(10);
    root->left = new Node(5);
    root->right = new Node(5);
    root->left->left = new Node(2);
    root->right->right = new Node(2);

  	if(isSymmetric(root)) {
      cout << "true";
    }
  	else cout << "false";

    return 0;
}
Java
import java.util.LinkedList;
import java.util.Queue;

// Node Structure
class Node {
    int data;
    Node left, right;

    Node(int val)
    {
        data = val;
        left = right = null;
    }
}

class GfG {

    static boolean isSymmetric(Node root)
    {
        if (root == null) {
            return true;
        }

        Queue<Node> q = new LinkedList<>();

        // Initialize the queue with the left and right
        // subtrees
        q.offer(root.left);
        q.offer(root.right);

        while (!q.isEmpty()) {

            Node node1 = q.poll();
            Node node2 = q.poll();

            // If both nodes are null, continue to the next
            // pair
            if (node1 == null && node2 == null) {
                continue;
            }

            // If one node is null and the other is not,
            // or the nodes' data do not match
            // then the tree is not symmetric
            if (node1 == null || node2 == null
                || node1.data != node2.data) {
                return false;
            }

            // Enqueue children in opposite order to compare
            // them
            q.offer(node1.left);
            q.offer(node2.right);
            q.offer(node1.right);
            q.offer(node2.left);
        }

        return true;
    }

    public static void main(String[] args) {

        // Creating a sample symmetric binary tree
        //       10
        //       / \
        //      5   5
        //     /     \
        //    2       2
        Node root = new Node(10);
        root.left = new Node(5);
        root.right = new Node(5);
        root.left.left = new Node(2);
        root.right.right = new Node(2);

        if (isSymmetric(root))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
from collections import deque

# Node Structure
class Node:
    def __init__(self, val=0, left=None, right=None):
        self.data = val
        self.left = left
        self.right = right

def isSymmetric(root):
    if root is None:
        return True

    q = deque()

    # Initialize the queue with the left and right subtrees
    q.append(root.left)
    q.append(root.right)

    while q:
        node1 = q.popleft()
        node2 = q.popleft()

        # If both nodes are None, continue
        if node1 is None and node2 is None:
            continue

        # If only one is None or values don't match, it's not symmetric
        if node1 is None or node2 is None or node1.data != node2.data:
            return False

        # Enqueue children in opposite order
        q.append(node1.left)
        q.append(node2.right)
        q.append(node1.right)
        q.append(node2.left)

    return True

if __name__ == "__main__":
    
    #  Creating a sample symmetric binary tree
    #           10
    #           / \
    #          5   5
    #         /     \
    #        2       2
    root = Node(10)
    root.left = Node(5)
    root.right = Node(5)
    root.left.left = Node(2)
    root.right.right = Node(2)
    
    print("true" if isSymmetric(root) else "false")
C#
using System;
using System.Collections.Generic;

// Node Structure
class Node {
    public int data;
    public Node left, right;

    public Node(int val) {
        data = val;
        left = right = null;
    }
}

class GFG {
  
    static bool isSymmetric(Node root) {
        if (root == null) {
            return true;
        }

        Queue<Node> q = new Queue<Node>();

        // Initialize the queue with the 
      	// left and right subtrees
        q.Enqueue(root.left);
        q.Enqueue(root.right);

        while (q.Count > 0) {
          
            Node node1 = q.Dequeue();
            Node node2 = q.Dequeue();

            // If both nodes are null, 
          	// continue to the next pair
            if (node1 == null && node2 == null) {
                continue;
            }

            // If one node is null and the other is not, 
            // or the nodes' data do not match
            // then the tree is not symmetric
            if (node1 == null || node2 == null || 
                node1.data != node2.data) {
                return false;
            }

            // Enqueue children in opposite 
          	// order to compare them
            q.Enqueue(node1.left);
            q.Enqueue(node2.right);
            q.Enqueue(node1.right);
            q.Enqueue(node2.left);
        }

        return true;
    }

    static void Main() {
      
        // Creating a sample symmetric binary tree
        //       10
        //       / \
        //      5   5
        //     /     \
        //    2       2
        Node root = new Node(10);
        root.left = new Node(5);
        root.right = new Node(5);
        root.left.left = new Node(2);
        root.right.right = new Node(2);

        Console.WriteLine(isSymmetric(root) ? "true" : "false");
    }
}
JavaScript
// Node Structure
class Node {
    constructor(val) {
        this.data = val;
        this.left = this.right = null;
    }
}

// Simple Queue class
class Queue {
    constructor() {
        this.items = [];
        this.head = 0;  
    }

    enqueue(val) {
        this.items.push(val);
    }

    dequeue() {
        if (this.isEmpty()) return null;
        const val = this.items[this.head];
        this.head++;
        // Optional: clean up memory if head is large
        if (this.head > 50) {
            this.items = this.items.slice(this.head);
            this.head = 0;
        }
        return val;
    }

    isEmpty() {
        return this.head >= this.items.length;
    }
}

function isSymmetric(root) {
    if (root === null) {
        return true;
    }

    const q = new Queue();

    // Initialize the queue with the left
    // and right subtrees
    q.enqueue(root.left);
    q.enqueue(root.right);

    while (!q.isEmpty()) {
    
        const node1 = q.dequeue();
        const node2 = q.dequeue();

        // If both nodes are null, 
        // continue to the next pair
        if (node1 === null && node2 === null) {
            continue;
        }

        // If one node is null and the other is not, 
        // or the nodes' data do not match
        // then the tree is not symmetric
        if (node1 === null || node2 === null || 
            node1.data !== node2.data) {
            return false;
        }

        // Enqueue children in opposite 
        // order to compare them
        q.enqueue(node1.left);
        q.enqueue(node2.right);
        q.enqueue(node1.right);
        q.enqueue(node2.left);
    }
    return true;
}

// Driver Code


// Creating a sample symmetric binary tree
//       10
//       / \
//      5   5
//     /     \
//    2       2
let root = new Node(10);
root.left = new Node(5);
root.right = new Node(5);
root.left.left = new Node(2);
root.right.right = new Node(2);

console.log(isSymmetric(root) ? "true" : "false");

Output
true


Comment