P4913 【深基16.例3】二叉树深度

https://www.luogu.com.cn/problem/P4913

#include<bits/stdc++.h>
using namespace std;
struct node{
    int l, r;
};
node a[1000005];
int n, ans=-1;
void dfs(int root, int deep){
    if(root==0)return;
    ans=max(ans,deep);
    dfs(a[root].l, deep+1);
    dfs(a[root].r, deep+1);
}
int main()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>a[i].l>>a[i].r;
    dfs(1,1);
    cout<<ans;
    
    return 0;
 } 

 

posted @ 2020-08-12 17:31  TFLSNOI  阅读(208)  评论(0编辑  收藏  举报