Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Symmetric Tree problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Symmetric Tree problem solution, we have given the root of a binary tree, check whether it is a mirror of itself.

Leetcode Symmetric Tree problem solution

Leetcode Symmetric Tree problem solution in Python.

class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        if root is None:
            return True
        stack = [(root, root)]
        
        while stack: 
            node1, node2 = stack.pop()
            if node1 is None and node2 is None:
                pass
            elif node1 is None or node2 is None:
                return False
            elif node1.val != node2.val:
                return False
            else:    
                stack.append((node1.left, node2.right))
                stack.append((node1.right, node2.left))
            
        return True

Symmetric Tree problem solution in Java.

class Solution
{
	public boolean isSymmetric(TreeNode root) 
	{
		Queue<TreeNode> q = new LinkedList<>();
		q.add(root);
		q.add(root);

		while(!q.isEmpty()) 
		{
			TreeNode t1 = q.poll();
			TreeNode t2 = q.poll();

			if (t1 == null && t2 == null) 
				continue;
			
			if (t1 == null || t2 == null || t1.val != t2.val)
				return false;
			
			q.add(t1.left);
			q.add(t2.right);
			q.add(t1.right);
			q.add(t2.left);
		}
		return true;
	}
}

Problem solution in C++.

class Solution {
public:

bool sameTree(TreeNode* lr,TreeNode* rr){
    
    if(!lr && !rr)
        return true;
    
    if(!lr || !rr)
        return false;
    
    if(lr->val != rr->val)
        return false;
    
    return sameTree(lr->left,rr->right) && sameTree(lr->right,rr->left);
}

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

Problem solution in C.

bool dfs(struct TreeNode *root1, struct TreeNode* root2) {
    if(root1 == NULL && root2 == NULL) {
        return true;
    }
    
    if(root1 && root2) {
        if(root1->val == root2->val) {
            return (dfs(root1->left, root2->right) && dfs(root1->right, root2->left));
        }
    }
    
    return false;
}

bool isSymmetric(struct TreeNode* root){
    return dfs(root, root);
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes