提交时间:2024-01-22 22:20:40

运行 ID: 123406

#include <bits/stdc++.h> using namespace std ; struct tr { int lk , rk ; } ; tr tree [114514] ; int maxn = -1 ; void dfs ( int x , int cnt ) { if ( tree [x] . lk == 0 && tree [x] . rk == 0 ) { maxn = max ( maxn , cnt ) ; return ; } if ( tree [x] . lk ) { dfs ( tree [x] . lk , cnt + 1 ) ; } if ( tree [x] . rk ) { dfs ( tree [x] . rk , cnt + 1 ) ; } return ; } int main ( ) { int n ; cin >> n ; for ( int i = 1 ; i <= n ; i ++ ) { cin >> tree [i] . lk >> tree [i] . rk ; } dfs ( 1 , 1 ) ; cout << maxn << endl ; return 0 ; }