还是找到中序遍历的结点,进行建树,最后bfs输出就好
和06差不多,细节注意下。
AC代码:
/* ***********************************************
Author :yzkAccepted
Created Time :2016/7/11 20:28:05
TASK :ggfly.cpp
LANG :C++
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
const int maxn=50;
int mid[maxn],fr[maxn];
struct node{
int l,r;
}a[maxn];
int build(int la,int ra,int lb,int rb)//la,ra表示中序遍历 lb,rb表示前序遍历
{
if(la>ra)
return 0;
int rt=fr[lb],p1,p2;
p1=la;
while(mid[p1]!=rt) p1++;//在前序遍历中找到根节点
p2=p1-la;
a[rt].l=build(la,p1-1,lb+1,lb+