304 North Cardinal St.
Dorchester Center, MA 02124

# 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``````

Constraints:

• 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;
if(left.val!=right.val)
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
else:
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
else:
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 {
public:
bool isSymmetric(TreeNode *root) {
TreeNode *left, *right;
if (!root)
return true;

queue<TreeNode*> q1, q2;
q1.push(root->left);
q2.push(root->right);
while (!q1.empty() && !q2.empty()){
left = q1.front();
q1.pop();
right = q2.front();
q2.pop();
if (NULL == left && NULL == right)
continue;
if (NULL == left || NULL == right)
return false;
if (left->val != right->val)
return false;
q1.push(left->left);
q1.push(left->right);
q2.push(right->right);
q2.push(right->left);
}
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

##### Conclusion:

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