提交时间:2024-01-22 20:32:35

运行 ID: 123323

#include<iostream> using namespace std; int maxs; struct Node{ int cl; int cr; }tree[107]; void dfs(int dep,int id){ //当前深度为dep,已遍历到编号为id的点 if(tree[id].cl==0 && tree[id].cr==0){ maxs=max(maxs,dep); return ; } if(tree[id].cl!=0){ dfs(dep+1,tree[id].cl); } if(tree[id].cr!=0){ dfs(dep+1,tree[id].cr); } } int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ cin>>tree[i].cl>>tree[i].cr; } dfs(1,1); cout<<maxs<<endl; return 0; }