Cod sursă (job #178971)

Utilizator avatar theodor.moroianu theodor moroianu theodor.moroianu IP ascuns
Problemă Pointeri Compilator cpp | 0.98 kb
Rundă Arhiva de probleme Status evaluat
Dată 9 ian. 2016 11:21:28 Scor 60
#include <fstream>
using namespace std;

int arb_st[200001], arb_dr[200001], v[200001];
int prec = -1, prim;

void inordine_st(int nod);
void inordine_dr(int nod);
ofstream out("pointeri.out");

int main() {
	int n, rad;
	ifstream in("pointeri.in");
	in >> n >> rad;
	for (int i = 0; i < n; i++)
		in >> arb_st[i];
	for (int i = 0; i < n; i++)
		in >> arb_dr[i];
	int x;
	for (x = rad; arb_st[x] != -1; x = arb_st[x])
		;
	out << x << '\n';
	inordine_st(rad);
	for (int i = 0; i < n; i++)
		out << v[i] << ' ';
	out << '\n';
	prec = rad;
	inordine_dr(rad);
	v[prec] = -1;
	for (int i = 0; i < n; i++)
		out << v[i] << ' ';
	
	out.close();
	return 0;
}

void inordine_dr(int n) {
	if (arb_st[n] != -1)
		inordine_dr(arb_st[n]);
	v[prec] = n;
	prec = n;
	if (arb_dr[n] != -1)
		inordine_dr(arb_dr[n]);
}

void inordine_st(int n) {
	if (arb_st[n] != -1)
		inordine_st(arb_st[n]);
	v[n] = prec;
	prec = n;
	if (arb_dr[n] != -1)
		inordine_st(arb_dr[n]);
}