fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int n;
  7. vector<int> in, post;
  8.  
  9. void func(int is, int ie, int ps, int pe) {
  10. cout << is << " " << ie << " " << ps << " " << pe << endl;
  11. if(is >= ie || ps >= pe)
  12. return;
  13.  
  14. int root = post[pe-1];
  15. int ri = find(in.begin(), in.end(), root)-in.begin();
  16. cout << ri << " " << root << endl;
  17.  
  18. int ln = ri-is;
  19.  
  20. func(is, ri, ps, ps+ln);
  21. func(ri+1, ie, ps+ln+1, pe-2);
  22. }
  23.  
  24. int main() {
  25. cin >> n;
  26. in.resize(n);
  27. post.resize(n);
  28. //인 오더
  29. //포스트 오더
  30. for(int i = 0; i < n; i++)
  31. cin >> in[i];
  32. for(int i = 0; i < n; i++)
  33. cin >> post[i];
  34.  
  35. //포스트 오더의 맨 뒤가 루트
  36. //=> 인오더에서 루트의 좌 우가
  37. func(0, n, 0, n);
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0.01s 5436KB
stdin
n
1 2 3
1 3 2
stdout
0 0 0 0