题解 | #二叉树遍历#
二叉树遍历
https://www.nowcoder.com/practice/6e732a9632bc4d12b442469aed7fe9ce
#include <iostream> #include <vector> using namespace std; string a, b; vector<char>res; int na, nb; void dfs(int la, int ra, int lb, int rb) { if(la > ra || lb > rb) return; int mid; for (int i = lb; i <= rb; i++) { if(b[i] == a[la]) { mid = i; break; } } dfs(la + 1, la + mid -lb, lb, mid - 1); dfs(la + mid -lb + 1, ra, mid + 1, rb); res.push_back(a[la]); } int main() { while(cin >> a >> b) { res.clear(); na = a.length(); nb = b.length(); dfs(0, na - 1, 0, nb - 1); for (int i = 0; i < res.size(); i++) cout << res[i]; cout << endl; } } // 64 位输出请用 printf("%lld")