代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef struct Node {
struct Node* left;
struct Node* right;
int depth;
} N;
int main() {
int n;
cin>>n;
N nodes[n];
int L,R;
int M_D=0;
for(int i=0; i<n; i++) {
cin>>L>>R;
if(R==0) {
nodes[i].right=NULL;
}
else {
nodes[i].right= &nodes[R-1];
nodes[i].right->depth=nodes[i].depth+1;
}
if(L==0) {
nodes[i].left=NULL;
}
else {
nodes[i].left= &nodes[L-1];
nodes[i].left->depth=nodes[i].depth+1;
}
if(i==0) {
nodes[i].depth=1;
}
if(nodes[i].depth>M_D) {
M_D=nodes[i].depth;
}
}
cout<<M_D;
return 0;
}
请大佬指点