-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdenticalBinaryTree.cpp
More file actions
47 lines (40 loc) · 941 Bytes
/
IdenticalBinaryTree.cpp
File metadata and controls
47 lines (40 loc) · 941 Bytes
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
/*Question:
Identical Binary Trees
Asked in:
Amazon
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Return 0 / 1 ( 0 for false, 1 for true ) for this problem
Example :
Input :
1 1
/ \ / \
2 3 2 3
Output :
1 or True
Seen this question in a real interview before
*/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
int Solution::isSameTree(TreeNode* A, TreeNode* B) {
if(A==NULL and B==NULL)
return 1;
if(A==NULL)
return 0;
if(B==NULL)
return 0;
if(A->val != B->val)
return 0;
if(A->val == B->val)
{
if(isSameTree(A->left,B->left) and isSameTree(A->right,B->right))
return 1;
}
}