Symmetric Tree – Leetcode Solution

In this post, we are going to solve the 101. Symmetric Tree problem of Leetcode. This problem 101. Symmetric Tree is a Leetcode easy level problem. Let’s see the code, 101. Symmetric Tree – Leetcode Solution.

Problem

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

Now, let’s see the code of 101. Symmetric Tree – Leetcode Solution.

Symmetric Tree – Leetcode Solution

101. Symmetric Tree – Solution in Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    public boolean isSymmetricRec(TreeNode left, TreeNode right){
        
        if(left == null || right == null){
            return left == right;
        }
        if(left.val != right.val) return false;
        
        return isSymmetricRec(left.left,right.right) && isSymmetricRec(right.left,left.right);
        
    }
    
    public boolean isSymmetric(TreeNode root) {
        return root == null || isSymmetricRec(root.left,root.right);
    }
}

101. Symmetric Tree – Solution in C++

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        
        if(root==NULL) return true;
        
        return isSymmetricTest(root->left,root->right);
    }
    
    bool isSymmetricTest(TreeNode* p , TreeNode* q){
        if(p == NULL && q == NULL) 
            return true; 
        
        else if(p == NULL || q == NULL)
            return false; 
        
        else if(p->val!=q->val) 
            return false;
        
        return isSymmetricTest(p->left,q->right) && isSymmetricTest(p->right,q->left);
    }
};

101. Symmetric Tree – Solution in Python

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def isSymmetric(self, root):
        if not root:
            return True
        return self.dfs(root.left, root.right)

    def dfs(self, l, r):
        if l and r:
            return l.val == r.val and self.dfs(l.left, r.right) and self.dfs(l.right, r.left)
        return l == r

Note: This problem 101. Symmetric Tree is generated by Leetcode but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

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