Tree (中序+后序还愿树,并求根到哪个叶的和最小)

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.

Input

The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.

Output

For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.

Sample Input

3 2 1 4 5 7 6

3 1 2 5 6 7 4

7 8 11 3 5 16 12 18

8 3 11 7 16 18 12 5

255

255

Sample Output

1

3

255

题意:给了二叉搜索树的中序,后序遍历结果,求原树中,从根节点到叶节点的最小和,如果有多个结果输出叶节点最小的那个值。

思路:这道题主要是考察根据中序和后序遍历结果还原树,然后dfs求值即可。中序遍历是 左->根->右  后序遍历是 左->右->根。我们在后序遍历结果中从后往前依次选择一个值,每次正好可以在中序遍历中把以此节点为根的子树的左右子树找出。以此还原树。

代码如下:

#include<cstdio>
#include<cstring>
#include<malloc.h>
#define N 100010
int zh[N],ho[N],cns,minn,flag;
struct node
{
    int val;
    node *lch,*rch;
};
node *insert(int x,int y,int l) //x为后序遍历中某处的下标,y为中序的下标,l为区间长度
{
    if(l<=0)  //长度小于等于0,这个区间已找完,此点为空
        return NULL;
    node *root=(node*)malloc(sizeof(node)); //开辟一个动态储存空间
    root->val=ho[x]; //此点为根节点
    int p;
    for(int i=y; i>=0; i--)  //在中序遍历中找到相同值的位置
        if(zh[i]==ho[x])
        {
            p=i;
            break;
        }
    int k=y-p; //区间大小
    root->rch=insert(x-1,y,k); //右子树
    root->lch=insert(x-k-1,p-1,l-k-1); //左子树
    return root;
}
int dfs(node *root,int sum) //dfs找根到叶节点的最短路径长度,sum为长度
{
    if(root!=NULL) //不为空
    {
        int x=dfs(root->lch,sum+root->val);//左子树
        int y=dfs(root->rch,sum+root->val);//右子树
        if(x&&y) //x和y都为1,表明这个节点没有左右儿子,为叶节点
        {
            if(sum+root->val<minn) //维护最短
            {
                minn=sum+root->val;
                flag=root->val;
            }
            else if(sum+root->val==minn) //长度相等时,要叶节点最小的值
                flag=root->val<flag?root->val:flag;
        }
        return 0;//此节点有值,返回0
    }
    else //此节点没有值,返回1
        return 1;
}
int main()
{
    char s[N];
    while(gets(s))
    {
        int l=strlen(s);
        int a=0,b=0;
        for(int i=0; i<l; i++) //字符串处理得到中序遍历结果
        {
            if(s[i]==' ')
            {
                zh[b++]=a;
                a=0;
            }
            else
                a=a*10+s[i]-'0';
        }
        zh[b++]=a;
        for(int i=0; i<b; i++)  //由于已经知道树的大小,可以直接输入后序遍历结果
            scanf("%d",&ho[i]);
        getchar();
        cns=b;
        node *root=(node*)malloc(sizeof(node));//开辟一个动态空间
        root=insert(b-1,b-1,b); //把后序,中序的末下标引入
        minn=0x3f3f3f3f;  //最短路径长度初始化
        int x=dfs(root,0);  //dfs找最短路经长度
        printf("%d\n",flag);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值