POJ-2255 Tree Recovery

mac2024-09-29  55

POJ-2255 Tree Recovery

题目链接:POJ-2255 题目大意:给定一棵树的前序遍历和中序遍历,让你输出这颗树的后序遍历

解题思路:二分法的思想????

代码块:

#include<iostream> #include<string> using namespace std; string pre, mid; int n = -1; void solve(int beginIndex, int endIndex){ if(beginIndex > endIndex) return; n++; int i; for(i = beginIndex; i <= endIndex; i++){ if(pre[n] == mid[i]) break; } solve(beginIndex, i - 1); solve(i + 1, endIndex); cout<<mid[i]; } int main() { while(cin>>pre>>mid) { solve(0, pre.length() - 1); n = -1; cout<<endl; } return 0; }
最新回复(0)