Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 71181b6

Browse files
트리의 순회
1 parent 09ec758 commit 71181b6

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

‎README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
| 적록색약 | 🟩 | 깊이우선 탐색 | [C++](https://github.com/techbless/algorithm-playground/blob/master/challenges/BOJ10026.md) | [문제](https://www.acmicpc.net/problem/10026) |
2929
| 제곱 ᄂᄂ 수 | 🟥 | 에라토스테네스의 체 | [C++](https://github.com/techbless/algorithm-playground/blob/master/challenges/BOJ1016.md) | [문제](https://www.acmicpc.net/problem/1016) |
3030
| RGB 거리 | 🟨 | 다이나믹 프로그래밍 | [C++](https://github.com/techbless/algorithm-playground/blob/master/challenges/BOJ1149.md) | [문제](https://www.acmicpc.net/problem/1149) |
31-
| 트리의 지름 | 🟨 | 그래프 이론 | [C++](https://github.com/techbless/algorithm-playground/blob/master/challenges/BOJ1167.md) | [문제](https://www.acmicpc.net/problem/1167)
31+
| 트리의 지름 | 🟨 | 그래프 이론 | [C++](https://github.com/techbless/algorithm-playground/blob/master/challenges/BOJ1167.md) | [문제](https://www.acmicpc.net/problem/1167) |
32+
| 트리의 순회 | 🟥 | 분할 정복 | [C++](https://github.com/techbless/algorithm-playground/blob/master/challenges/BOJ2263.md) | [문제](https://www.acmicpc.net/problem/2263) |
33+
3234

3335
## 자료구조 & 알고리즘
3436

‎challenges/BOJ2263.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 문제
2+
트리의 순회
3+
## 문제 원본
4+
문제의 원본은 [여기서](https://www.acmicpc.net/problem/2263) 확인하세요.
5+
6+
## 분류
7+
* 분할정복
8+
9+
# 풀이
10+
11+
트리를 직접 그려보면, `post-order`의 경우 항상 마지막 값이 `root`값이라는 것을 알수 있다.
12+
13+
`post-order`에서 `root`값을 찾은후 출력하고 `position`에 저장된 `in-order``root`위치를 찾는다.
14+
15+
그 후, 왼쪽 노드의 사이즈를 구한다. 왼쪽 노드와 오른쪽 노드를 분할정복한다.
16+
17+
이를 반복하면 `pre-order`를 구할 수 있다.
18+
19+
``` c++
20+
#include <iostream>
21+
#include <vector>
22+
23+
#define MAX 100001
24+
25+
using namespace std;
26+
27+
int in[MAX];
28+
int post[MAX];
29+
int position[MAX];
30+
31+
void input(int n) {
32+
for (int i = 0; i < n; i++) {
33+
cin >> in[i];
34+
}
35+
36+
for (int i = 0; i < n; i++) {
37+
cin >> post[i];
38+
}
39+
40+
for (int i = 0; i < n; i++) {
41+
position[in[i]] = i;
42+
}
43+
}
44+
45+
void solve(int inS, int inE, int postS, int postE) {
46+
if (inS > inE || postS > postE) {
47+
return;
48+
}
49+
50+
int root = post[postE];
51+
cout << root << " ";
52+
53+
int p = position[root];
54+
int left_size = p - inS;
55+
56+
solve(inS, p - 1, postS, postS + left_size + -1);
57+
solve(p + 1, inE, postS + left_size, postE - 1);
58+
}
59+
60+
int main(void) {
61+
ios::sync_with_stdio(false);
62+
cin.tie(0); cout.tie(0);
63+
64+
int n;
65+
cin >> n;
66+
67+
input(n);
68+
69+
solve(0, n - 1, 0, n - 1);
70+
71+
return 0;
72+
}
73+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /