BOJ 1991 트리 순회

1 개요[ | ]

BOJ 1991 트리 순회

2 C++[ | ]

#include <iostream>
using namespace std;

int N;
int A[26][2];

void preOrder(char start) {
    if(start == '.') return;
    cout << start;
    preOrder(A[start][0]);
    preOrder(A[start][1]);
}

void inOrder(char start) {
    if(start == '.') return;
    inOrder(A[start][0]);
    cout << start;
    inOrder(A[start][1]);
}

void postOrder(char start) {
    if(start == '.') return;
    postOrder(A[start][0]);
    postOrder(A[start][1]);
    cout << start;
}

void solve() {
    preOrder('A');
    cout << "\n";
    inOrder('A');
    cout << "\n";
    postOrder('A');
    cout << "\n";
}

int main() {
    cin >> N;
    char c1, c2, c3;
    for(int i=0; i<N; i++) {
        cin >> c1 >> c2 >> c3;
        A[c1][0] = c2;
        A[c1][1] = c3;
    }
    solve();
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}