Posts Identical Trees (geeksforgeeks - SDE Sheet)
Post
Cancel

Identical Trees (geeksforgeeks - SDE Sheet)

PROBLEM DESCRIPTION

Given two binary trees, the task is to find if both of them are identical or not.

geeksforgeeks

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution
{

    boolean isIdentical(Node root1, Node root2)
    {

        if(root1 == null && root2 == null)
            return true;

        if(root1 == null || root2 == null)
            return false;

        if(root1.data != root2.data)
            return false;

        return isIdentical(root1.left, root2.left) && isIdentical(root1.right, root2.right);

    }

}
This post is licensed under CC BY 4.0 by the author.