40分求帮忙看看哪的问题
查看原帖
40分求帮忙看看哪的问题
1330072
Relisc楼主2024/7/17 22:42
#include <bits/stdc++.h>
using namespace std;
int n; 
int son[2][1001000];
int l1=1,r1=1; 

int  depth(int u){
	//左子树不空,递归,每进入一层,层数+1 
	if(son[0][u])	  l1=	depth(son[0][u])+1;
	//右子树不空,递归,每进入一层,层数+1 
	if(son[1][u])	  r1=	depth(son[1][u])+1;
	
	//左右都空,返回左右子树中最大的层数 
	return	max(l1,r1);
}

int main(){
	//
	//son[0][0]=son[1][0]=1;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int l,r;
		cin>>l>>r;
		if(l)	son[0][i]=l;
		if(r)	son[1][i]=r;
	}
	cout<<depth(1);
	return 0;
}

2024/7/17 22:42
加载中...