1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include <stdio.h> #include <vector> #include <utility> using namespace std; vector<pair<int, int> > arr(30); int ctoi(char c) { return (int)(c - 'A' + 1); } char itoc(int n) { return (char)(n + 'A' - 1); } void preorder(int c); void inorder(int c); void postorder(int c); int main() { int n; scanf("%d", &n); getchar(); int t = n; while (n--) { char a, b, c; int idx1 = 0, idx2 = 0, idx3 = 0; scanf("%c %c %c", &a, &b, &c); getchar(); if ('A' <= a && a <= 'Z') idx1 = ctoi(a); if ('A' <= b && b <= 'Z') idx2 = ctoi(b); if ('A' <= c && c <= 'Z') idx3 = ctoi(c); arr[idx1].first = idx2; arr[idx1].second = idx3; } preorder(1); printf("\n"); inorder(1); printf("\n"); postorder(1); printf("\n"); } void preorder(int n) { if (n == 0) return; char alpha = itoc(n); printf("%c", alpha); preorder(arr[n].first); preorder(arr[n].second); } void inorder(int n) { if (n == 0) return; inorder(arr[n].first); char alpha = itoc(n); printf("%c", alpha); inorder(arr[n].second); } void postorder(int n) { if (n == 0) return; postorder(arr[n].first); postorder(arr[n].second); char alpha = itoc(n); printf("%c", alpha); } | cs |
'알고리즘 > DFS' 카테고리의 다른 글
9466 텀 프로젝트(hard) (0) | 2019.09.03 |
---|---|
2668 숫자 고르기 (0) | 2019.09.03 |
17412 도시 왕복하기 1 (0) | 2019.08.22 |
11377 열혈강호3 (0) | 2019.08.22 |
6086 최대 유량 (0) | 2019.08.22 |