2012 - 二叉树好简单
有一棵二叉树,我们知道其知道先序和中序遍历的顺序,请你求出后其后序遍历的顺序。
Input
先序遍历和中序遍历得到的顺序,每个节点用一个英文大写字母表示(所以节点数不会超过26)
Output
其后序遍历的顺序。
Examples
Input
DBACEGF ABCDEFG BCAD CBAD
Output
ACBFGED CDAB
Solution C++
#include <bits/stdc++.h> using namespace std; void f(string a, string b) { if (a.size() == 0) return; char root = a[0]; int pos = b.find(root); f(a.substr(1, pos), b.substr(0, pos)); f(a.substr(pos + 1), b.substr(pos + 1)); cout << root; } int main() { string a, b; while (cin >> a >> b) { f(a, b); cout << endl; } return 0; }