티스토리 뷰

Algorithm/BOJ

5639 이진 검색 트리

henry1214 2018. 11. 13. 22:05

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



preorder의 순서인 root->left->right을 이용해서 재귀적으로 범위를 설정해서 postorder의 순서인 left->right->root로 바꾼다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdio>
 
int n,a[10001];
 
void go(int l,int r)
{
    if(l>r) return;
    int m=l;
    while(a[++m]<a[l]);
    go(l+1,m-1);
    go(m,r);
    printf("%d\n",a[l]);
}
 
int main()
{
    while(scanf("%d",&a[n])!=EOF) n++;
    a[n]=1e6;
    go(0,n-1);
    return 0;
}
cs


'Algorithm > BOJ' 카테고리의 다른 글

14003 가장 긴 증가하는 부분 수열 5  (0) 2018.11.14
12015 가장 긴 증가하는 부분 수열 2  (0) 2018.11.14
1174 줄어드는 숫자  (0) 2018.09.20
1038 감소하는 수  (0) 2018.09.20
15971 두 로봇  (0) 2018.09.16
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday