×

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

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that...

4 minutes read.

Extended Binary Tree

A form of binary tree known as an extended binary tree replaces all of the original tree's null subtrees with special nodes known as external nodes, while the remaining nodes...

4 minutes read.

Sum of Nodes in a Binary Tree

In this article, we will see the sample problems that will help us understand the concept and summation of all the nodes in the binary tree. Implementation /* creating a program that...

4 minutes read.

Left View of Binary Tree

Implementation // creating a C++ program to print the Left view of the binary tree. #include <bits/stdc++.h> using namespace std; struct Nod { int record; struct Nod *Lft, *Rt; }; // creating a utility function that will eventually help...

4 minutes read.

Doubly Linked List

Doubly Linked List Doubly linked list is another kind of Linked list. Doubly linked list contains two pointers for navigation. In this, we can traverse the list in both directions, either...

4 minutes read.

What is the Use of Segment Trees in Data Structure?

Segment trees Segment trees are also called statistical trees in computer science. They are a type of tree data structure. Segment trees are used to store information regarding segments and intervals....

6 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Invert binary tree

Invert binary tree is a mirror image of a tree. It is pretty much the same compared to the only difference: its left and right children are swapped with the...

4 minutes read.

Common Operations on various Data Structures

Data structures are ways to organise data in computer memory for quick and effective use. The storage of data uses a variety of data-structures. It is also possible to define...

7 minutes read.

Top view of binary tree

We know that a binary tree is a kind of tree that helps us organize our tree and that it is a kind of non-linear info structure that at least...

4 minutes read.

Graph Data Structure

A graph is a non-primitive and non-linear data structure. It is a group of (V, E) where V is a set of vertexes, and E is a set of edge....

3 minutes read.

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language. Implementation #include <iostream> using namespace std; class B_TreeNod {   int *kys;   int m;   BTreeNod **C;   int j;   bool leaf;  ...

5 minutes read.

Given a Binary Tree Check the Zig-Zag Traversal

Implementation // The C++ implementation of the zig-zag traversal method in the O(n) time.  #include <iostream> #include <stack> using namespace std; // creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a...

4 minutes read.

Dynamic memory allocation of structure in C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

5 minutes read.

Extended Binary Tree

An extended binary tree is a binary tree in which all the NILL subtrees present mainly in the original trees are exchanged with the special nodes that are primarily known...

3 minutes read.

Hashing and its Applications

Hashing Hashing refers to transforming plain text data in such a way that even if it is leaked for some reason, no one would be able to make sense of it....

6 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.

Operations on 2D-Arrays

Two Dimensional Array Operations Adding Elements to Two-D Arrays We must put data in both rows and columns when inserting items in 2-D Arrays. As a result, we employ the idea of...

10 minutes read.

Insertion sort

Insertion sort is a simple sorting technique. It is best suited for small data sets, but it does not suitable for large data sets. In this technique, we pick an...

4 minutes read.

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some...

4 minutes read.