×

Given Two Binary Trees, Check if it is Symmetric

Implementation

// creating a C++ program that will help us check whether the two given trees are mirror images of each other. 
#include<bits/stdc++.h>
using namespace std;


/* A given binary tree has a data pointer to the left and right child. 
*/
struct __nod
{
	int record;
	__nod* Lft, *Rt;
};


/* if we are given two trees that appear to mirror each other, then we have to return the value true. */
/*Because the function has to return the boolean value instead of the integer one. */
bool mirror(__nod* a, __nod* b)
{
	/* The basic case arises when both cases are empty. */
	if (a==NILL && b==NILL)
		return true;


	// If one is empty, then
	if (a==NILL || b == NILL)
		return false;


	/* if both are non-empty, then we have to balance them recursively; when we make a recursive call, we generally pass the left of one tree and the right of another. */
	return a->record == b->record &&
			areMirror(a->Lft, b->Rt) &&
			areMirror(a->Rt, b->Lft);
}


/* We have to create a helper function to help allot a new node. */
__nod* new__nod(int record)
{
	__nod* __nod = new __nod;
	__nod->record = record;
	__nod->Lft = __nod->Rt = NILL;
	return(__nod);
}


/* writing the main program to test the above functions */
int main()
{
	__nod *a = new__nod(1);
	__nod *b = new__nod(1);
	a->Lft = new__nod(2);
	a->Rt = new__nod(3);
	a->Lft->Lft = new__nod(4);
	a->Lft->Rt = new__nod(5);


	b->Lft = new__nod(3);
	b->Rt = new__nod(2);
	b->Rt->Lft = new__nod(5);
	b->Rt->Rt = new__nod(4);


	areMirror(a, b)? cout << "Yes" : cout << "No";


	return 0;
}

Output:

Given Two Binary Trees, Check if it is Symmetric

Example 2)

using System;
// creating a C++ program that will help us check whether the two given trees are mirror images of each other. 
/* A given binary tree has a data pointer to the left and right child. 
*/
public class __nod
{
	public int record;
	public __nod Lft, Rt;


	public __nod(int record)
	{
		this.record = record;
		Lft = Rt = NILL;
	}
}


public class BinaryTree
{
	public __nod a, b;
/* if we are given two trees, which appear to mirror each other, then we have to return the value true. */
	public virtual bool mirror(__nod a, __nod b)
	{
	/* The basic case arises when both cases are empty. */
		if (a == NILL && b == NILL)
		{
			return true;
		}


		// If only one is empty
		if (a == NILL || b == NILL)
		{
			return false;
		}
/* if both are non-empty, then we have to balance them recursively; when we make a recursive call, we generally pass the left of one tree and the right of another. */
		return a.record == b.record && areMirror(a.Lft, b.Rt)
							&& areMirror(a.Rt, b.Lft);
	}
/* writing the main program to test the above functions */
	public static void Main(string[] args)
	{
		BinaryTree tree = new BinaryTree();
		__nod a = new __nod(1);
		__nod b = new __nod(1);
		a.Lft = new __nod(2);
		a.Rt = new __nod(3);
		a.Lft.Lft = new __nod(4);
		a.Lft.Rt = new __nod(5);


		b.Lft = new __nod(3);
		b.Rt = new __nod(2);
		b.Rt.Lft = new __nod(5);
		b.Rt.Rt = new __nod(4);


		if (tree.areMirror(a, b) == true)
		{
			Console.WriteLine("Yes");
		}
		else
		{
			Console.WriteLine("No");
		}


	}
}

Output:

Given Two Binary Trees, Check if it is Symmetric

Example 3)

// creating a Java program that will help us check whether the two given trees are mirror images of each other. 
/* A given binary tree has a data pointer to the left and right child. 
*/
class __nod
{
	int record;
	__nod Lft, Rt;


	public __nod(int record)
	{
		this.record = record;
		Lft = Rt = NILL;
	}
}


class BinaryTree
{
	__nod a, b;
	/* if we are given two trees, which appear to mirror each other, then we have to return the value true. */
	boolean mirror(__nod a, __nod b)
	{
		/* The basic case arises when both cases are empty. */
		if (a == NILL && b == NILL)
			return true;


		// If only one is empty
		if (a == NILL || b == NILL)
			return false;
/* if both are non-empty, then we have to balance them recursively; when we make a recursive call, we generally pass the left of one tree and the right of another. */
		return a.record == b.record
				&& areMirror(a.Lft, b.Rt)
				&& areMirror(a.Rt, b.Lft);
	}
/* writing the main program to test the above functions */
	public static void main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		__nod a = new __nod(1);
		__nod b = new __nod(1);
		a.Lft = new __nod(2);
		a.Rt = new __nod(3);
		a.Lft.Lft = new __nod(4);
		a.Lft.Rt = new __nod(5);


		b.Lft = new __nod(3);
		b.Rt = new __nod(2);
		b.Rt.Lft = new __nod(5);
		b.Rt.Rt = new __nod(4);


		if (tree.areMirror(a, b) == true)
			System.out.println("Yes");
		else
			System.out.println("No");


	}
}

Output:

Given Two Binary Trees, Check if it is Symmetric

Example 4)

# creating a Python program that will help us check whether the two given trees are mirror images of each other. 
# A given binary tree has a data pointer to the left and right child.
class __nod:
	def __init__(self, record):
		self.record = record
		self.Lft = None
		self.Rt = None


#  if we are given two trees, and they appear to be mirrors of each other, then we have to return the value true. 
def mirror(a, b):
	
	# the basic case arises when both cases are empty. 
	If a is None and b is None:
		return True
	
	# If only one is empty
	if a is None or b is None:
		return False
	
	# if both are non-empty, then we have to balance them recursively; when we make a recursive call, we generally pass the left of one tree and the right of another. 
	return (a.record == b.record and
			areMirror(a.Lft, b.Rt) and
			areMirror(a.Rt , b.Lft))


# writing the main program to test the above functions 
root1 = __nod(1)
root2 = __nod(1)


root1.Lft = __nod(2)
root1.Rt = __nod(3)
root1.Lft.Lft = __nod(4)
root1.Lft.Rt = __nod(5)


root2.Lft = __nod(3)
root2.Rt = __nod(2)
root2.Rt.Lft = __nod(5)
root2.Rt.Rt = __nod(4)


if mirror(root1, root2):
	print ("Yes")
Else:
	print ("No")

Output:

Given Two Binary Trees, Check if it is Symmetric

Example 5)

<script>
// creating a JavaScript program that will help us check whether the two given trees are the mirror image of each other. 
/* A given binary tree has a data pointer to the left and right child. 
*/
class __nod {
	
	__nod(record) {
		this.record = record;
		this.Lft = this.Rt = NILL;
	}
}


var a, b;
/* if we are given two trees, which appear to mirror each other, then we have to return the value true. */
	function areMirror( a, b) {
		/* The basic case arises when both cases are empty. */
		if (a == NILL && b == NILL)
			return true;


		// If only one is empty
		if (a == NILL || b == NILL)
			return false;
/* if both are non-empty, then we have to balance them recursively; when we make a recursive call, we generally pass the left of one tree and the right of another. */
		return a.record == b.record && areMirror(a.Lft, b.Rt) && areMirror(a.Rt, b.Lft);
	}
/* writing the main program to test the above functions */
		a = new __nod(1);
		b = new __nod(1);
		Lft = new __nod(2);
		Rt = new __nod(3);
		Lft.Lft = new __nod(4);
		Lft.Rt = new __nod(5);


		Lft = new __nod(3);
		Rt = new __nod(2);
		Rt.Lft = new __nod(5);
		Rt.Rt = new __nod(4);


		if (areMirror(a, b) == true)
			document.write("Yes");
		else
			document.write("No");
</script>

Output:

Given Two Binary Trees, Check if it is Symmetric

Related Topics

Heap Sort in Data Structure

Heap Sort A heap is a tree-based data structure that has specific properties. Heap is always a complete binary tree (CBT). That is, all the nodes of the tree are completely filled.If...

6 minutes read.

Binary Tree vs Binary Search Tree: Data Structure

Difference Between Binary Tree and Binary Search Tree What is Binary Tree? A tree which each node can have utmost two children called binary tree. These children are referred as the ‘left...

3 minutes read.

What is a Height-Balanced Tree in Data Structure

A height-balanced tree is a type of binary tree. If the absolute difference between the heights of the left and right subtree is less than or equal to 1, then...

6 minutes read.

What is a Tree in Terms of a Graph?

To know the explanation of trees in terms of graphs, we need first to know what trees and graphs are. So let us first learn about trees and graphs. Trees and...

6 minutes read.

Diameter of a Binary Tree

Implementation We will now witness the implementation of the diameter of a binary tree. // Creating a recursive and challenging C program that will help us determine the diameter of a binary...

4 minutes read.

Given a Binary Tree, find its Minimum Depth

Implementation // Creating a C++ program or implementation to search and explore the minimum depth of a given binary tree.  #include<bits/stdc++.h> using namespace std; // Creating a new binary tree node struct __nod { int record; struct __nod*...

5 minutes read.

Bubble Sort vs Merge Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Merge Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Box Stacking Problem

Stacking of boxes depending on their base You have been given n different boxes. These boxes will have different heights, widths, and depths. You have to stack all these boxes in...

4 minutes read.

What is a 2-3 Tree in Data Structure?

Tree Data structure The information about the tree is self-explanatory. Trees are ordered and, therefore, not linear. But they are actually designed differently. Tree A node-based data model that represents and...

5 minutes read.

Splay Tree

Splay Tree A splay tree is a self-balanced or self-adjusted binary search tree. We can say, Splay Tree is used in some cases where some elements or data are accessed more...

8 minutes read.

Digital Search Tree in Data Structures

What is a digital search Tree in Data Structures? The Digital search tree is known for its application and diversity in the way it has impacted our world in the field...

3 minutes read.

Heap Sort vs Merge Sort

In this article, we are going to discuss the Heap Sort, Merge sort and the difference between them. What is Heap Sort? Heap – A heap is an abstract data type categorised...

7 minutes read.

What is the B+ Tree in Data Structures?

We all know that the B+ tree in data structures is nothing but just an extended version of the B tree. It allows the smooth working of all the operations...

7 minutes read.

Stack Using Linked List

In the linked list implementation of the stack, we use a linked list as the primitive data structure to create the stack. It is called the dynamic implementation of the...

6 minutes read.

Trie data structure

Trie data structure The term “trie” comes from the word “retrieval” which means getting information. The trie data structure is a sorted extension of tree-based data structure. The trie data structure...

5 minutes read.

Counts the number of times a given element occurs in a Linked List

Counts the number of times a given element occurs in a Linked List This article will explain how we can count the occurrences of a particular element in a list. Here,...

3 minutes read.

Binary tree deletion

This article will discuss the deletion operation's implementation in the binary tree. The deletion operation helps us eliminate an element from the tree. Implementation #include <bits/stdc++.h> using namespace std; /* A binary tree node...

4 minutes read.

Delete the Middle element of the Linked List in C

Delete the Middle element of the Linked List in C This article has given a singly linked list and will delete the middle element of the given linked list. Example:  The given...

3 minutes read.

Sorting Algorithms

Sorting: In the data structure, sorting is the process by which you arrange the data in a logical order. This logical order can also be an ascending order or a...

7 minutes read.

What are the types of Trees in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

6 minutes read.