-
Notifications
You must be signed in to change notification settings - Fork 1
/
zad10_ms.c
58 lines (52 loc) · 1.33 KB
/
zad10_ms.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct node *bin_tree;
struct node
{
int val;
bin_tree left, right;
};
bin_tree create_node(int val)
{
bin_tree node = (bin_tree)(malloc(sizeof(int) + 2*sizeof(bin_tree)));
node->val=val;
return node;
}
bool spacer(bin_tree t, int depth, int *max_depth)
{
if (t == NULL)
return true;
if (t->left == t->right)
{
// leaf
if (*max_depth < depth)
return false;
*max_depth = depth;
}
return spacer(t->left, depth + 1, max_depth) && spacer(t->right, depth + 1, max_depth);
}
bool ultraleft(bin_tree t)
{
int maxDepth = 1;
bin_tree left = t->left;
while(left != NULL){
maxDepth++;
left = left->left;
}
return spacer(t, 1, &maxDepth);
}
int main()
{
bin_tree tree = create_node(1);
tree->left = create_node(2);
tree->left->left = create_node(3);
tree->left->right = create_node(4);
tree->left->right->right = create_node(5);
tree->left->right->right->right = create_node(3);
//tree->left->right->right->right->right = create_node(3);
//tree->left->right->right->right->right->right = create_node(3);
tree->left->left->left = create_node(6);
tree->left->left->left->left = create_node(7);
printf("%d\n", ultraleft(tree));
}