Symmetric Tree(leetcode,cpp):

 Symmetric Tree(leetcode,cpp):

 

 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

    1
   / \
  2   2    ==>>mirror of itself
 / \ / \
3  4 4  3
    1
   / \
  2   2    ==>> not mirror of iitself
   \   \
   3    3 

 =============================================

code:-

class Solution {
public:
    bool ismirror(TreeNode *node1,TreeNode* node2)
    {
        if(node1==NULL && node2==NULL)
            return true;
        if(node1==NULL || node2==NULL)
            return false;
         return node1->val==node2->val && ismirror(node1->left,node2->right) && ismirror(node1->right,node2->left);
    }
   
    bool isSymmetric(TreeNode* root)
    {
        if(root==NULL)
            return true;
       
       return ismirror(root->left,root->right);
      
    }
};

Comments

Popular posts from this blog

Amazing Subarrays(cpp,interviewbit)

3Sum(cpp,leetcode)