1

Gooooogle  •  2个月前


#include <iostream>
#include <cmath>
using namespace std;

struct node {
	int l, r;
} a[1000005]; //记录每个结点的左右结点
int Max = -1, n;

void dfs(int root, int step) {
	if (root == 0)
		return;//无根
	Max = max(Max, step);
	dfs(a[root].l, step + 1);
	dfs(a[root].r, step + 1);
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].l >> a[i].r;
	}
	dfs(1, 1); //从1号结点,深度为1开始搜索
	cout << Max; //输出最大值
	return 0;
}

include

include

using namespace std;

struct node {

int l, r;

} a[1000005]; //记录每个结点的左右结点 int Max = -1, n;

void dfs(int root, int step) {

if (root == 0)
	return;//无根
Max = max(Max, step);
dfs(a[root].l, step + 1);
dfs(a[root].r, step + 1);

}

int main() {

cin >> n;
for (int i = 1; i <= n; i++) {
	cin >> a[i].l >> a[i].r;
}
dfs(1, 1); //从1号结点,深度为1开始搜索
cout << Max; //输出最大值
return 0;

}



评论:

请先登录,才能进行评论