Skip to content

Commit

Permalink
트리의 순회
Browse files Browse the repository at this point in the history
  • Loading branch information
jungwoo1208 authored Jun 22, 2024
1 parent fd4e477 commit bcdf5b6
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Boj2263.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <vector>
using namespace std;

void tree(const vector<int>& in, const vector<int>& post, int in_start, int in_end, int post_start, int post_end) {
if (post_start > post_end) {
return;
}
int root = post[post_end];
cout << root << " ";
if (in_start == in_end) {
return;
}
int idx1 = 0;
for (int i = in_start; i <= in_end; i++) {
if (in[i] == root) {
idx1 = i;
break;
}
}
int left_size = idx1 - in_start;

tree(in, post, in_start, idx1 - 1, post_start, post_start + left_size - 1);
tree(in, post, idx1 + 1, in_end, post_start + left_size, post_end - 1);
}

int main() {
int n;
cin >> n;
vector<int> in(n), post(n);
for (int i = 0; i < n; i++) {
cin >> in[i];
}
for (int i = 0; i < n; i++) {
cin >> post[i];
}
tree(in, post, 0, n - 1, 0, n - 1);
return 0;
}

0 comments on commit bcdf5b6

Please sign in to comment.