forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
872.c
43 lines (32 loc) · 1.01 KB
/
872.c
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool leafSimilar(struct TreeNode* root1, struct TreeNode* root2){
int sequence1[100] ={0}, sequence2[100]={0}, num_of_node1 =0,num_of_node2 =0;
num_of_node1 = sequence(root1,sequence1,num_of_node1);
num_of_node2 = sequence(root2,sequence2,num_of_node2);
if (num_of_node1 != num_of_node2)
return false;
for (int i = 0;i<num_of_node1; i++){
if (sequence1[i] != sequence2[i])
return false;
}
return true;
}
int sequence(struct TreeNode* root ,int *list , int num_of_node){
if (!root)
return num_of_node;
if (!root->left && !root->right){
list[num_of_node] = root->val;
num_of_node ++;
return num_of_node;
}
num_of_node = sequence(root->left ,list , num_of_node);
num_of_node = sequence(root->right ,list , num_of_node);
return num_of_node;
}