Topic: Tree
1) Determine if Two Trees are Identical | Easy |
RECURSIVE SOLUTION
Input:
1 1
/ \ / \
2 3 2 3
Output: Yes
Explanation: There are two trees both
having 3 nodes and 2 edges, both trees
are identical having the root as 1,
left child of 1 is 2 and right child
of 1 is 3.
boolean isIdentical(Node root1, Node root2)
{
if((root1==null)&&(root2==null)){
return true;
}
else if((root1==null)||(root2==null)){
return false;
}
else{
return (root1.data==root2.data)&&isIdentical(root1.left,root2.left)&&isIdentical(root1.right,root2.right);
}
}
Thank you for reading :)