No Rules Rules

이진 검색 트리 (feat. 백준, 5639번) 본문

생활/코테

이진 검색 트리 (feat. 백준, 5639번)

개발하는 완두콩 2022. 8. 30. 19:36
728x90
반응형

이진 검색 트리
https://www.acmicpc.net/problem/5639

 

5639번: 이진 검색 트리

트리를 전위 순회한 결과가 주어진다. 노드에 들어있는 키의 값은 106보다 작은 양의 정수이다. 모든 값은 한 줄에 하나씩 주어지며, 노드의 수는 10,000개 이하이다. 같은 키를 가지는 노드는 없다

www.acmicpc.net

 

반응형

 

// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
using namespace std;
int arr[10001];
int ans[10001];
void solution(register int s, register int e) {
	if (s >= e)
		return;
	register int i = 0;
	for (i = s + 1; i < e; ++i)
		if (arr[s] < arr[i])
			break;

	solution(s + 1, i);
	solution(i, e);
	cout << arr[s] << "\n";
}
int main(void) {
	ios::sync_with_stdio(false), cin.tie(NULL);
	register int value, cnt = 0;
	while (cin >> value) {
		if (value == EOF)
			break;
		arr[cnt++] = value;
	}
	solution(0, cnt);
	return 0;
}
// *&)*@*
728x90
반응형
Comments