Skip to main content

Symmetric and Identical Trees

In this blog, we're going to cover Symmetric Tree and Identical Tree problems. These are quite similar problems, in the Symmetric tree we are provided with a single tree, but for checking identical trees we're provided two trees.

What is a Symmetric Tree?
A tree is called symmetric if it contains a mirror image of itself when divided from the center(root). This means that the left portion of the tree should be symmetric to the right portion of the tree.
Example of a Symmetric Tree:


What are Identical Trees?
Two trees are said to be identical when they have the same data and the arrangement of data is also the same in them.
Example of Identical Trees:

In symmetric trees, the left portion of the tree is compared to the right portion of the tree. Whereas in the case of identical trees the left portion of one tree is compared with the left portion of another tree similarly, the right portion of one tree is compared with the right portion of another tree.

Implementation

Symmetric Tree

// utility function
bool is_symmetric(node *root1,node *root2)
{
    if(root1==NULL && root2==NULL)
        return true;
    if(root1 && root2 && root1->data==root2->data)
        return is_symmetric(root1->left,root2->right) && is_symmetric(root1->right,root2->left);
return false;
}

// main function
bool symmetric(node *root)
{
    return is_symmetric(root,root);
}    

Identical Trees

// main function
bool isIdentical(Node *r1, Node *r2)
{
  if(!r1 && !r2)
  return true;
  if(r1 && r2)
  {
    if(r1->data==r2->data)
     return ( isIdentical(r1->left,r2->left) && isIdentical(r1->right,r2->right));
  }
return false;    
}
I hope this tutorial helped you!
Happy Coding!

Comments

Most Popular Posts

Left & Right View Of a Binary Tree Made Easy!

When talking about binary trees then the view of a binary tree is something that has the most probability to be asked in a coding interview. In this blog post, I'm going to talk mainly about the Right & Left views of a binary tree. There are so many ways available on the internet, that it's really a very difficult task to choose which method you should follow. Suppose, you're using one method for Left View and another method for Right View, now you need to know two different methods for two similar questions. In this blog we'll discuss a single approach by which you can solve both Left View and Right View problems.   Before diving directly into the approach we need to understand what exactly is this Right/Left view. For your better understanding I've created a binary tree : So let's try to understand the right view for this binary tree. Right View simply means looking at the tree from its right side and the only nodes visible from that view are required to b...

On Which Coding Platform Should I Practice?

Which is the best online platform to practice, how to improve my coding skills? This post is all about giving answers to all such questions. If you're a beginner who has got no idea what programming is then I would recommend you step by step practice any programming language on Hackerrank. Hackerrank has modules that are self-explanatory and would slowly make you understand all the terminologies which are generally used and with practice, you'll be better at it. Moreover, if you're still not able to understand anything you can always search for your answers on GeekForGeeks or HackerEarth both of them provide one of the best tutorials on any topic. If you're an intermediate who's comfortable with any programming language then you can always improve your problem-solving skills studying Data Structures and Algorithms and practicing it. You can also improve by competitive coding. The best place to start with competitive programming according to me would be ...

How to Invert A Binary Tree (Iteratively + Recursively)?

Inverting a binary is one of the most common questions being asked these days. Often, seen many times in the ad of AlgoExpert as well. So, what is an inverted binary tree? Inverted binary tree is a simply a mirror image of given binary tree. Example : So, all we need to do is " Swapping left children with right children of a binary tree at every level." Implementation Recursively TreeNode* invertTree(TreeNode* root) { if(root==NULL) return NULL; swap(root->left,root->right); invertTree(root->left); invertTree(root->right); return root; } Iteratively TreeNode* invertTree(TreeNode* root) { if(root==NULL) return NULL; queue<TreeNode*> q; q.push(root); while(!q.empty()) { TreeNode* c= q.front(); q.pop(); swap(c->left,c->right); if(c->left!=NULL) q.push(c->left); if(c->right!=NULL) q.push(c->right); } return root; } I hope this tutorial hel...