Learn-dsa..in 30 days!



























CC-6 : Check Binary Tree Equality.

Description:

Two Binary Tree are equal if they have the same structure of nodes and value of corresponding nodes is also the same. Given two input Binary Trees, check if the same are equal /Identical.

Test cases and expected outputs:

Input Parameters Expected outputs

Are two trees equal: false

Pseudocode:

Method equality(root1, root2)

If node data of root 1 and root 1 and root 2 is different return false.
Recursively call method checkEquality () with left child of root1 and left child of root2. If return from these recursive calls is false, return false.
Recursively call method checkEquality () with right child of root1 and right child of root2. If return from these recursive calls is false, return false.


Code:

public boolean checkEquality(BinTreeNode root1, BinTreeNode root2) {
	if ((root1==null)&&(root2==null)){
		return true;		
	}
	if ((root1==null)||(root2==null)){
		return false;
		
	}
	return ((root1.getNodeData()==root2.getNodeData())
			&&checkEquality(root1.getLeftChild(), root2.getLeftChild())
			&& checkEquality(root1.getRightChild(), root2.getRightChild()));	
}

Click here to download and run code and test cases !