A1086-Tree Traversals Again

几乎和1020一样,还是要弄清楚建树的问题,是个套路问题

#include
using namespace std;
struct node
{
    int data;
    node* left;
    node* right;
};
int N;
vector in,pre;
node* create(int preL,int preR,int inL,int inR) //序列区间 
{
    if(preL>preR)
        return NULL;
    node* root=new node;
    root->data=pre[preL];
    int i;
    for(i=inL;i<=inR;i++)
        if(in[i]==pre[preL])
            break;
    int numLeft=i-inL;
    root->left=create(preL+1,preL+numLeft,inL,i-1);
    root->right=create(preL+numLeft+1,preR,i+1,inR);
    return root;
}
int num=0;
void postorder(node* root)
{
    if(root==NULL)
        return;
    postorder(root->left);
    postorder(root->right);
    printf("%d",root->data);
    num++;
    if(num st;
    getchar();
    for(int n=0;n

你可能感兴趣的:(A1086-Tree Traversals Again)