http://ac.jobdu.com/problem.php?pid=1385
蛮怀旧的题目,记得大一就见过一直没做过,没难度,纯小心吧。
类似的是有中序和后续构建二叉树。比如http://www.cnblogs.com/kaituorensheng/p/3788533.html
思路很简单 递归构造:
#include <cstdio> #include <cstring> #include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int SIZE = 1000+10; struct Node{ int v; Node *left; Node *right; Node(){ v = -1; left = right = NULL; } Node(int val, Node *l, Node *r){ v = val; left = l; right = r; } }; int n; int head[SIZE],mid[SIZE]; int flag,getNode; Node* buildTree(int headptr, int midleft, int midright){ /*cout << "*********" << endl; cout << "head=" << head[headptr] << endl; cout << "midleft=" << midleft << endl; cout << "midright=" << midright << endl;*/ Node *father = new Node(); int pos=-1; if(midleft == midright){ if(head[headptr] == mid[midleft]){ father->left = father->right = NULL; father->v = head[headptr]; getNode++; return father; }else{ return NULL; } } if(midleft > midright)return NULL; for(int i=midleft;i<=midright;i++){ if(head[headptr] == mid[i]) pos = i; } if(pos == -1)return NULL; int hasleft=0;//,hasright=0; if(pos == midleft){//意味着左子树为空 father->left = NULL; }else{ hasleft = 1; father->left = buildTree( headptr+1 , midleft, pos-1); if(father->left == NULL)return NULL; } if(pos == midright){ father->right = NULL; }else{ if(hasleft == 1){ //左子树结点个数 pos-midleft father->right = buildTree(headptr+pos-midleft+1, pos+1, midright); }else{ father->right = buildTree(headptr+1, pos+1, midright); } if(father->right == NULL)return NULL; } father->v = head[headptr]; getNode++; return father; } /*void print(Node *nod){ if(flag)putchar(' '); else flag=1; printf("%d", nod->v); }*/ void print(Node *nod){ //if(flag)putchar(' '); //else flag=1; printf("%d ", nod->v); } void sear(Node *father){ //if(father->left == NULL && father->right == NULL){//叶子 //print(father); //} if(father->left)sear(father->left); if(father->right)sear(father->right); print(father); } int main(){ //freopen("06.txt", "r", stdin); int in; while(~scanf("%d", &n)){ flag = getNode = 0; for(int i=0;i<n;i++){ scanf("%d", &in); head[i] = in; } for(int i=0;i<n;i++){ scanf("%d", &in); mid[i] = in; } Node *ancestor = buildTree(0, 0, n-1); if(ancestor == NULL || getNode != n){ //if(ancestor == NULL ){ printf("No\n"); }else{ sear(ancestor); printf("\n"); } //cout << getNode << endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-01 02:41:50