-
[백준] 1991번 트리 순회 c++Coding Test/BaekJoon 2023. 12. 13. 10:54728x90
문제 설명
제한 사항
시간 제한 : 2초
메모리 제한 : 128MB
풀이
#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; int arr[2][27]; void preOrder(int x, string& str) { if (x < 0) { return; } str.push_back(x + 'A'); preOrder(arr[0][x], str); preOrder(arr[1][x], str); } void inOrder(int x, string& str) { if (x < 0) { return; } inOrder(arr[0][x], str); str.push_back(x + 65); inOrder(arr[1][x], str); } void postOrder(int x, string& str) { if (x < 0) { return; } postOrder(arr[0][x], str); postOrder(arr[1][x], str); str.push_back(x + 65); } int main() { int n; cin >> n; char value, left, right; for (int i = 0; i < n; i++) { cin >> value >> left >> right; arr[0][value - 'A'] = left - 'A'; arr[1][value - 'A'] = right - 'A'; } string str = ""; preOrder(0, str); cout << str << "\n"; str = ""; inOrder(0, str); cout << str << "\n"; str = ""; postOrder(0, str); cout << str << "\n"; return 0; }
728x90'Coding Test > BaekJoon' 카테고리의 다른 글
[백준] 문자열 9086번 c++, java, python (0) 2023.08.29 [백준] 입력 속도 비교 (0) 2023.05.10 [BaekJoon] 개똥벌레 c++ , java (0) 2023.02.01