-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path3_width_tree.c
executable file
·92 lines (88 loc) · 2.14 KB
/
3_width_tree.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
WIDTH_TREE
Assignment name : width_tree
Expected files : width_tree.c
Allowed functions:
--------------------------------------------------------------------------------
ALERT: OPTIMIZED SOLUTION REQUIRED.
Given the root node of a binary tree, create a function that returns the
'width' of the tree, which is the number of node present on the longest
path between two leaves in the tree.
The binary tree uses the following node structure :
struct s_node
{
int value;
struct s_node *left;
struct s_node *right;
};
The function must be declared as follows:
int width_tree(struct s_node *n);
Consideration:
- Be careful: the naive solution won't work on our big input, you have to find
an optimized solution which will run in O(N) time (where N is the number of nodes).
- The values of each node does not matter.
- The bigger test we will do is about 250 000 nodes. It should run in less
than 2 seconds.
Example 1:
1
/ \
2 \
/ \ \
3 4 5
/ / \
6 7 8
In this case, your function should return 6,
because the longest path between two leaves is 6->4->2->1->5->7 (or finish by 8),
which contains 6 nodes, so the tree have a 'width' of 6.
Example 2:
1
/ \
2 \
/ \ 3
4 \
/ \ \
5 6 7
\ / \
8 9 10
/ \ \
11 12 13
In this case, your function should return 7,
because the longest path between two leaves is 8->5->4->2->7->9->11 .
Example 3:
10
\
12
In this case, your function should return 2.
Example 4:
25
/
33
/ \
12 9
/
3
In this case, your function should return 4.
Example 5:
10
(here is a root with no children)
In this case, your function should return 1.
*/
struct s_node
{
int value;
struct s_node *left;
struct s_node *right;
};
#define max(a, b) (a > b ? a : b)
int longest(struct s_node *n){
if (!n)
return (0);
return max(longest(n->left), longest(n->right)) + 1;
}
int width_tree(struct s_node *n)
{
if (!n)
return (0);
int width = longest(n->left) + longest(n->right) + 1;
return max(max(width_tree(n->left), width_tree(n->right)), width);
}