Given two binary trees, the task is to find if both of them are identical or not.
Example 2:
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.
Example 2:
Input:
1 1
/ \ / \
2 3 3 2
Output: No
Explanation: There are two trees both
having 3 nodes and 2 edges, but both
trees are not identical.
Your task:
Since this is a functional problem you don't have to worry about input, you just have to complete the function isIdentical() that takes two roots as parameters and returns true or false. The printing is done by the driver code.
My Answer:
boolean isIdentical(Node root1, Node root2) { // Code Here boolean result1 = true; boolean result2 = true; if( root1 == null && root2 != null) return false; if( root1 != null && root2 == null) return false; if( root1 == null && root2 == null) return true; if (root1.data == root2.data) { result1 = isIdentical(root1.left, root2.left); result2 = isIdentical(root1.right, root2.right); } else { return false; } return result1 && result2; }
No comments:
Post a Comment