二叉树建树

给出前序和中序建树:

Node* build (int n, int* pre, int* in) {
    Node* node = new Node;
    int i = 0;
    if (n <= 0)
        return NULL;
    while (in[i] != pre[0])
        i++;
    node -> val = in[i];
    node -> left = build(i, pre + 1, in);
    node -> right = build(n - i - 1, pre + i + 1, in + i + 1);
    return node;
}

给出中序和后序建树:

Node* build (int n, int* in, int* pos) {
    Node* node = new Node;
    int i = n - 1;
    if (n <= 0)
        return NULL;
    while (in[i] != pos[n - 1])
        i--;
    node -> val = ino[i];
    node -> left = build(i, in, pos);
    node -> right = build(n - i - 1, in + i + 1, pos + i);
    return node;
}

UVA-548 给你一棵树的中序和后序遍历,求从根到叶子组成的路径中数字和最小的那条,输出最小路径的叶子。
思路:在重建完二叉树后,dfs遍历找到最小路径和叶子

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;
int in[10010],pos[10010];
int min1,ans;

struct tree
{
    int data;
    tree *l,*r;
};
tree *root;

tree* build(int n, int* in, int* pos)
{
    tree* node = new tree;
    int i=n-1;
    if (n<=0)
        return NULL;
    while (in[i]!=pos[n-1])
        i--;
    node->data=in[i];
    node->l=build(i,in, pos);
    node->r=build(n-i-1,in+i+1,pos+i);
    return node;
}
void dfs(tree *tr,int sum)
{
    if(tr==NULL)
    {
        return;
    }
    sum=sum+tr->data;
    if(tr->l==NULL&&tr->r==NULL)
    {
        if(min1>sum)
        {
            ans=tr->data;
            min1=sum;
        }
    }
    else
    {
        if(tr->l!=NULL)
        {
            dfs(tr->l,sum);
        }
        if(tr->r!=NULL)
        {
            dfs(tr->r,sum);
        }
    }
}

int main()
{
    char ch;
    while(scanf("%d%c",&in[0],&ch)!=EOF)
    {
        int n=1;
        for(int i=1;ch!='\n';i++)
        {
            scanf("%d%c",&in[i],&ch);
            n++;
        }
        ch = ' ';
        for(int i=0;ch!='\n';i++)
        {
            scanf("%d%c",&pos[i],&ch);
        }
        min1=1000001;
        root=build(n,in,pos);
        dfs(root,0);
        printf("%d\n",ans);
    }
    return 0;
}

杭电1710
根据前序遍历和中序遍历,输出后序遍历

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>

using namespace std;

const int N=1010;
int pre[N],in[N];

struct tree
{
    int data;
    tree *l,*r;
};
tree *root;
tree *Creattree(int *pre,int *in,int n)
{
    tree *tem;
    for(int i=0;i<n;i++)
    {
        if(pre[0]==in[i])
        {
            tem=(tree*)malloc(sizeof(tree));
            tem->data=in[i];
            tem->l=Creattree(pre+1,in,i);
            tem->r=Creattree(pre+i+1,in+i+1,n-(i+1));
            return tem;
        }
    }
    return NULL;
}

void Printfpos(tree *rt)
{
    if(rt!=NULL)
    {
        Printfpos(rt->l);
        Printfpos(rt->r);
        if(rt==root)
        {
            printf("%d\n",rt->data);
        }
        else
        {
            printf("%d ",rt->data);
        }
    }
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&pre[i]);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d",&in[i]);
        }
        root=Creattree(pre,in,n);
        Printfpos(root);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值