-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildIdenticalTrees.cpp
More file actions
39 lines (36 loc) · 853 Bytes
/
buildIdenticalTrees.cpp
File metadata and controls
39 lines (36 loc) · 853 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
#include<iostream>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int cntMatrix(TreeNode* A, TreeNode* B){
//make A equal to B by inserting nodes in A
if(!A && !B){
//both are null
return 0;
}
if(A && !B){
//if A is not null and B is null
return -1;
}
int returnValue = 0;
int left,right;
if(!A && B){
//means A is null
left = cntMatrix(NULL,B->left);
if(left==-1){
return -1;
}
right = cntMatrix(NULL,B->right);
if(right==-1){
return -1;
}
return left + right + 1;
}
//both are not null;
left = cntMatrix(A->left, B->left);
right = cntMatrix(A->right,B->right);
return left + right;
}