Coding Test/BaekJoon

[백준] 1991번 트리 순회 c++

owls 2023. 12. 13. 10:54
728x90

문제 설명

문제 바로가기

 

제한 사항

시간 제한 : 2초

메모리 제한 : 128MB

 

풀이

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int arr[2][27];

void preOrder(int x, string& str) {
	if (x < 0) {
		return;
	}
	str.push_back(x + 'A');
	preOrder(arr[0][x], str);
	preOrder(arr[1][x], str);
}

void inOrder(int x, string& str) {
	if (x < 0) {
		return;
	}
	inOrder(arr[0][x], str);
	str.push_back(x + 65);
	inOrder(arr[1][x], str);
}

void postOrder(int x, string& str) {
	if (x < 0) {
		return;
	}
	postOrder(arr[0][x], str);
	postOrder(arr[1][x], str);
	str.push_back(x + 65);
}

int main() {

	int n;
	cin >> n;

	char value, left, right;
	for (int i = 0; i < n; i++) {
		cin >> value >> left >> right;
		arr[0][value - 'A'] = left - 'A';
		arr[1][value - 'A'] = right - 'A';

	}
	
	string str = "";
	preOrder(0, str);
	cout << str << "\n";

	str = "";
	inOrder(0, str);
	cout << str << "\n";

	str = "";
	postOrder(0, str);
	cout << str << "\n";

	return 0;

}

 

728x90