中序

coder  •  1个月前


#include <bits/stdc++.h>
using namespace std;
int a[500010], b[500010], n = 0, j = 1, c = 0;

struct stu {
	int v = 0;
	int cl = 0;
	int cr = 0;
} a1[500010];

void zx (int id) {
	if (a1[id].cl != 0) {
		zx(a1[id].cl);
	}
	b[j] = a1[id].v;
	j++;
	if (a1[id].cr != 0) {
		zx(a1[id].cr);
	}
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a1[i].v >> a1[i].cl >> a1[i].cr;
		a[i] = a1[i].v;
	}
	sort(a + 1, a + 1 + n);
	zx(1);
	for (int i = 1; i <= n; i++) {
		if (a[i] == b[i]) {
			c++;
		}
	}
	cout << c;
}

include <bits/stdc++.h>

using namespace std; int a[500010], b[500010], n = 0, j = 1, c = 0;

struct stu {

int v = 0;
int cl = 0;
int cr = 0;

} a1[500010];

void zx (int id) {

if (a1[id].cl != 0) {
	zx(a1[id].cl);
}
b[j] = a1[id].v;
j++;
if (a1[id].cr != 0) {
	zx(a1[id].cr);
}

}

int main() {

cin >> n;
for (int i = 1; i <= n; i++) {
	cin >> a1[i].v >> a1[i].cl >> a1[i].cr;
	a[i] = a1[i].v;
}
sort(a + 1, a + 1 + n);
zx(1);
for (int i = 1; i <= n; i++) {
	if (a[i] == b[i]) {
		c++;
	}
}
cout << c;

}



评论:

请先登录,才能进行评论