-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheight-of-the-tree.cpp
More file actions
40 lines (37 loc) · 880 Bytes
/
height-of-the-tree.cpp
File metadata and controls
40 lines (37 loc) · 880 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int likeDFS(vector<int> &v, vector<int> &l, vector<int> &r, int start) {
int h = 0;
if (l[start] != -1) {
int left = likeDFS(v, l, r, l[start]);
h = max(h, left);
}
if (r[start] != -1) {
int right = likeDFS(v, l, r, r[start]);
h = max(h, right);
}
return h + 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
freopen("height.in", "r", stdin);
freopen("height.out", "w", stdout);
int n;
cin >> n;
if (n == 0) {
cout << 0 << endl;
return 0;
}
vector<int> v(n);
vector<int> l(n);
vector<int> r(n);
for (int i = 0; i < n; i++) {
cin >> v[i] >> l[i] >> r[i];
l[i]--, r[i]--;
}
cout << likeDFS(v, l, r, 0) << endl;
}