(c++) 5639_이진검색트리

이아름·2022년 12월 19일
0

코딩테스트

목록 보기
6/6
post-thumbnail

문제

https://www.acmicpc.net/problem/5639


풀이

visual studio에서 풀고 디버깅했는데 입력 부분에서 계속 안되길래..
왜 안되지 하고 삽질 했는데...
백준에 코드 그대로 돌리니 바로 통과되었다..ㅠㅠ

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

vector<int> pre;

void postOrder(int preL, int preR) {
	if (preL >= preR) {
		if (preL == preR) {
			cout << pre[preL] << "\n";
		}
		return;
	}
	int right = preL + 1;
	for (; right <= preR && pre[right] < pre[preL]; right++);
	postOrder(preL + 1, right - 1);
	postOrder(right, preR);
	cout << pre[preL] << "\n";
}

int main() {
	int n;
	while (cin >> n) {
		pre.push_back(n);
	}
	postOrder(0, pre.size() - 1);
	return 0;
}
profile
반갑습니다

0개의 댓글