洛谷_P4913 【深基16.例3】二叉树深度

洛谷_P4913 【深基16.例3】二叉树深度

//
#include<bits/stdc++.h>
using namespace std;

int ans;

struct node
{
    int x,y;
    node( int xx=0,int yy=0 ):x(xx),y(yy) {}
};
vector<node> v;

void dfs( int pos,int sum )
{
    if( pos==0 ) { ans=max( ans,sum ); return ; }
    dfs( v[pos].x,sum+1 );
    dfs( v[pos].y,sum+1 );
}

int main()
{
    int n,x,y;
    cin>>n;

    v.clear(); v.push_back( node() );

    while( n-- )                // 根节点为 1
    {
        cin>>x>>y;
        v.push_back( node( x,y ) );
    }
    ans=0; dfs( 1,0 );          // 1,0
    cout<<ans<<endl;
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_63173957/article/details/124955130
今日推荐