티스토리 뷰
2263번: 트리의 순회
첫째 줄에 n(1≤n≤100,000)이 주어진다. 다음 줄에는 인오더를 나타내는 n개의 자연수가 주어지고, 그 다음 줄에는 같은 식으로 포스트오더가 주어진다.
www.acmicpc.net
백준 소스코드 [C++] 2263 트리의 순회
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
typedef long long ll;
using namespace std;
vector<int> idx,post,in;
void preorder(int postL, int postR, int inL, int inR) {
if (postR < postL || inR < inL) return;
cout << post[postR] << " ";
preorder(postL, postL+idx[post[postR]]-inL-1,inL, idx[post[postR]] - 1);
preorder(postL + idx[post[postR]] - inL, postR-1, idx[post[postR]] +1,inR);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
idx.resize(n+1); post.resize(n+1); in.resize(n+1);
for (int i = 1;i <= n;i++) {
cin >> in[i];
idx[in[i]] = i;
}
for (int i = 1;i <= n;i++) cin >> post[i];
preorder(1,n,1,n);
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 2467 용액 (0) | 2021.03.02 |
---|---|
백준 소스코드 [C++] 12920 평범한 배낭 2 (0) | 2021.03.01 |
백준 소스코드 [C++] 11478 서로 다른 부분 문자열의 개수 (0) | 2021.02.27 |
백준 소스코드 [C++] 13925 수열과 쿼리 13 (0) | 2021.02.25 |
백준 소스코드 [C++] 11438 LCA 2 (0) | 2021.02.24 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 최단경로 알고리즘
- HIG
- 벨만포드 시간복잡도
- CompositionalLayout
- 에드몬드 카프 알고리즘
- 최단경로문제
- CPU와 Memory
- test coverage
- 컴퓨터 추상화
- WWDC19
- rxswift
- IOS
- State Restoration
- mach-o
- 코딩대회
- 벨만포드 알고리즘
- 최대 매칭
- 부스트캠프 6기
- 네트워크 유량
- 네트워크 플로우
- WWDC21
- 다익스트라 시간복잡도
- WWDC17
- observeOn
- 포드 풀커슨 알고리즘
- Testable
- MeTal
- 최단경로 문제
- WWDC16
- 강한 순환 참조
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함