Symmetric Tree LeetCode Solution – Queslers

Problem – Symmetric Tree LeetCode Solution

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

Example 1:

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

Example 2:

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


  • The number of nodes in the tree is in the range [1, 1000].
  • -100 <= Node.val <= 100

Symmetric Tree LeetCode Solution in Java

public boolean isSymmetric(TreeNode root) {
    return root==null || isSymmetricHelp(root.left, root.right);

private boolean isSymmetricHelp(TreeNode left, TreeNode right){
    if(left==null || right==null)
        return left==right;
        return false;
    return isSymmetricHelp(left.left, right.right) && isSymmetricHelp(left.right, right.left);

Symmetric Tree LeetCode Solution in Python

class Solution:
  def isSymmetric(self, root):
    if root is None:
      return True
      return self.isMirror(root.left, root.right)

  def isMirror(self, left, right):
    if left is None and right is None:
      return True
    if left is None or right is None:
      return False

    if left.val == right.val:
      outPair = self.isMirror(left.left, right.right)
      inPiar = self.isMirror(left.right, right.left)
      return outPair and inPiar
      return False

Symmetric Tree LeetCode Solution in C++

 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
class Solution {
    bool isSymmetric(TreeNode *root) {
        TreeNode *left, *right;
        if (!root)
            return true;
        queue<TreeNode*> q1, q2;
        while (!q1.empty() && !q2.empty()){
            left = q1.front();
            right = q2.front();
            if (NULL == left && NULL == right)
            if (NULL == left || NULL == right)
                return false;
            if (left->val != right->val)
                return false;
        return true;
Symmetric Tree LeetCode Solution Review:

In our experience, we suggest you solve this Symmetric Tree LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Symmetric Tree LeetCode Solution

Find on LeetCode


I hope this Symmetric Tree LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

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