树的建立

时间:2019-04-28 00:40:47   收藏:0   阅读:159

#include <cstdio> 
#include <iostream> 
using namespace std; 
const int N = 50; 
int pre[N], in[N], post[N];        int n; 
struct node { 
    int data;
    node* lchild; 
    node* rchild;
}; 
node* create(int prel, int prer, int inl, int inr)  {           
    if (prel>prer)                                { 
        return NULL; 
    } 
    node* root = new node;                         root->data = pre[prel];                       int k;                                     
    for (k = inl; k <= inr; k++) 
    { 
        if (in[k] == pre[prel])                            break; 
    } 
    int numleft = k - inl;                         root->lchild = create(prel + 1, prel + numleft, inl, k - 1);      
    root->rchild = create(prel + numleft + 1, prer, k + 1, inr);        return root;             

原文:https://www.cnblogs.com/kangqinming/p/10781175.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!