Advertisement
tepyotin2

BinaryTree

May 18th, 2025
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void func(string pre, string in, string &post){
  6.     if(pre.size() == 1){
  7.         post+=pre;
  8.         return;
  9.     }
  10.     int pos = in.find(pre[0]);
  11.     string left = in.substr(0, pos);
  12.     string tl = pre.substr(1, left.size());
  13.     string right = in.substr(pos+1);
  14.     string tr = pre.substr(tl.size()+1);
  15.     //cout << "pos: " << pos << '\n';
  16.     //cout << left << ", " << tl << ", " << right << ", " << tr << '\n';
  17.     if(left.size()>0){
  18.         func(tl, left, post);
  19.     }
  20.     if(right.size()>0){
  21.         func(tr, right, post);
  22.     }
  23.     post+=pre[0];
  24. }
  25.  
  26. int main(){
  27.     //freopen("binarytree.in", "r", stdin);
  28.    
  29.     string a, b;
  30.     while(cin >> a){
  31.         cin >> b;
  32.         string res = "";
  33.         func(a, b, res);
  34.         cout << res << '\n';
  35.     }
  36.    
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement