Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자료구조
- 투 포인터
- 정렬
- 구현
- 수학
- 유니온 파인드
- 유니티
- Team Fortress 2
- 시뮬레이션
- 다익스트라
- BFS
- 스택
- Unreal Engine 5
- DFS
- 그래프
- 다이나믹 프로그래밍
- 재귀
- 누적 합
- 브루트포스
- 트리
- 알고리즘
- c++
- 우선순위 큐
- 그리디 알고리즘
- 백트래킹
- ue5
- VR
- 백준
- 문자열
- XR Interaction Toolkit
Archives
- Today
- Total
1일1알
백준 2263번 트리의 순회 C++ 본문

포스트오더는 루트를 마지막에 순회하는것을 이용해서 인오더에서 루트값을 찾아서 작은 트리로 분할해가면서 루트를 출력하는 방식으로 풀었다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n;
vector<int> inOrderIndex;
vector<int> postOrder;
void PrintRoot(int inOrderStart, int inOrderEnd, int postOrderStart, int postOrderEnd) {
if (inOrderStart > inOrderEnd || postOrderStart > postOrderEnd)
return;
int root = postOrder[postOrderEnd];
int rootIndex = inOrderIndex[root];
int leftSize = rootIndex - inOrderStart;
int rightSize = inOrderEnd - rootIndex;
cout << root << " ";
PrintRoot(inOrderStart, rootIndex - 1, postOrderStart, postOrderStart + leftSize - 1);
PrintRoot(rootIndex + 1, inOrderEnd, postOrderEnd - rightSize, postOrderEnd - 1);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
inOrderIndex = vector<int>(n + 1);
postOrder = vector<int>(n + 1);
for (int i = 1; i <= n; i++) {
int input;
cin >> input;
inOrderIndex[input] = i;
}
for (int i = 1; i <= n; i++) {
int input;
cin >> input;
postOrder[i] = input;
}
PrintRoot(1, n, 1, n);
};
'알고리즘' 카테고리의 다른 글
백준 1238번 파티 C++ (0) | 2022.06.26 |
---|---|
백준 14938번 서강그라운드 C++ (0) | 2022.06.25 |
백준 1918번 후위 표기식 C++ (0) | 2022.06.21 |
백준 1167 트리의 지름 C++ (0) | 2022.06.20 |
백준 1865번 웜홀 C++ (0) | 2022.06.19 |