mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-10 07:58:52 +00:00
P4913 【深基16.例3】二叉树深度
R41386907
This commit is contained in:
parent
0a15257189
commit
1ad1d8d5a7
27
problem/P4913/P4913.cpp
Normal file
27
problem/P4913/P4913.cpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int n, l[1000005], r[1000005];
|
||||||
|
|
||||||
|
int dfs(int now, int cnt) {
|
||||||
|
if (l[now] == 0 && r[now] == 0) {
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
else if (l[now] == 0) {
|
||||||
|
return dfs(r[now], cnt + 1);
|
||||||
|
}
|
||||||
|
else if (r[now] == 0) {
|
||||||
|
return dfs(l[now], cnt + 1);
|
||||||
|
}
|
||||||
|
return max(dfs(l[now], cnt + 1), dfs(r[now], cnt + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
cin >> n;
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
cin >> l[i] >> r[i];
|
||||||
|
}
|
||||||
|
cout << dfs(1, 1) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user