P4913 【深基16.例3】二叉树深度https://www.luogu.com.cn/problem/P4913
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
#include <cctype>
#include <cstdlib>
#include <deque>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
struct node {
int l, r;
};
node tree[MAXN];
int ans;
inline void dfs(int id, int deep) {
if (id == 0) {
return;
}
ans = max(ans, deep);
dfs(tree[id].l, deep + 1);
dfs(tree[id].r, deep + 1);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d %d", &tree[i].l, &tree[i].r);
}
dfs(1, 1);
printf("%d", ans);
return 0;
}