-
Notifications
You must be signed in to change notification settings - Fork 1
/
Determine_if_Two_Trees_are_Identical.cpp
49 lines (41 loc) · 1.33 KB
/
Determine_if_Two_Trees_are_Identical.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Problem Statement:
-----------------
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.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
*/
// Link --> https://practice.geeksforgeeks.org/problems/determine-if-two-trees-are-identical/1
// Code:
class Solution
{
public:
bool isIdentical(Node *r1 , Node *r2)
{
if(r1 == NULL and r2 == NULL)
return true;
if(r1!=NULL and r2!=NULL)
return (r1->data == r2->data) and
isIdentical(r1->left , r2->left) and isIdentical(r1->right , r2->right);
return false;
}
};