这里只给出后序和中序确定二叉树的方法,前序和中序是差不多的;
先给出代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<queue>
#include<map>
#include<sstream>
#include<vector>
#include<string>
#define MAXN 1000
using namespace std;
struct Node {
int n;
Node *left,*right;
Node():left(NULL),right(NULL),n(-1) {}
};
Node *root;
int inO[1001],post[1001],cnt;
Node * newnode() { return new Node(); }
bool read_data(int *a) {
string tmp;
if(!getline(cin,tmp)) return false;
stringstream s(tmp);
cnt = 0;
int x;
while(s >> x) a[cnt++] = x;
return true;
}
Node *buildtree(int *inO,int *post,int len) {
if(len == 0) return NULL;
Node *node = newnode();
node->n = post[len - 1];
int p = 0;
for(; p < len ; ++p) {
if(inO[p] == post[len - 1]) break;