Function to check if two Binary Tree are Identical or not

1. This can done easily using recursive method. Here is complete function .

//check to binary tree is identical

static bool isBinaryTreeIdentical(treeNode *rootNode1, treeNode *rootNode2) {

	if (rootNode1 == NULL && rootNode2 == NULL)
		return true;
	else if (rootNode1 == NULL || rootNode2 == NULL)
		return false;
	else
		return (rootNode1->val == rootNode2->val
				&& isBinaryTreeIdentical(rootNode1->leftChild,
						rootNode2->leftChild)
				&& isBinaryTreeIdentical(rootNode1->rightChild,
						rootNode2->rightChild));

}

};

above mention function is member function of my Tree ADT class you can check complete program here

A Developer
Gy@n!

About Gyaneshwar Pardhi

Exploring the world and trying to know how can i involve to make this perfect. Gy@n!
This entry was posted in Algorithm, c++, Data Structure, interview Question, Programming and tagged , . Bookmark the permalink.

Leave a comment