Check if 2 binary tree are identical

In this chapter, we shall see if 2 trees are identical.

Problem statement: 

You are given root node of 2 binary trees, you need to check if 2 trees are identical.
Example:
Consider the below image
check_if_2_binary_trees_are_identical
The first set are identical; the second set are not identical.
From the image we can derive below true cases:
1. If both nodes are null, then return true
2. If the right node value of tree 1 is equal to the right node value of tree 2, return true.
3. If the Left node value of tree 1 is equal to the Left node value of tree 2, return true.
From the image we can derive below false cases:

1. If a node from tree 1 has value, and node from tree 2 is null, then return false.
2. If both the tree nodes have a value, but they are different, then return false.
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *