uva 548 - Tree

  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出最价值最小的路径,输出的是最小路径的叶子节点;

首先我们来看下面的二叉树:

用中序遍历和后序遍历创建二叉树的思想方法 - 清培 - 清培的博客
以此为基础,我们来实现我们二叉树创建程序。
我们先写出它的中序和后序遍历
中序:7 4 2 5 1 3 6
后序:7 4 5 2 6 3 1
很显然,我们要做的是这样下面事情:
1 找出后续数组的最后一个元素。
2 以后序数组的最后一个元素为根据,将中序数组分成左子树和右子树。
3 以中序数组的左子树右子树为根据,将后序数组分为左子树和右子树。
4 将中序左子树,后序左子树作为一个新的,中序数组和后序数组,进行递归。同时将新根数的根赋值给先前根的左儿子。
5 同理并同时处理右子树。
6 过程中通过判断语句,处理好没有儿子时的指针赋空问题和递归基线问题。
 
上面的方法是从比人那copy来的
自己实践遇到许多麻烦
a[ ] L R 7 4 2 5 1 3 6 
b[ ] l    r 7 4 5 2 6 3 1 
分别要对2组数据设置指针记录当前范围,
找出根节点在前序遍历的位置pos。进行左右划分,对左右子树递归求解
root=1 pos=5  kL=1; kR=7; kl=1; kr=7;
7 4 2 5 LR[1,4]  L=kL;        R=pos-1;
7 4 5 2 lr[1,4]  l=kl;        r=kr-(kR-pos)-1
    3 6   LR[6,7]  L=pos+1     R=kR;
6 3     lr[5,6]  l=kr-(kR-pos); r=kr-1;
 
当L>R  无左或右子树情况
当L=R 出现叶子节点; 

root=2 pos=3 pos2=r=4 L'=1; R'=4; l'=1; r'=4;
7 4    LR[1,2] 
7 4    lr[1,2]
   5    LR[4,4]
5     
最后dfs犯了个初始化错误就是初始化和值最大为所有节点和,叶子节点为根节点
但是出现3 4 5
                5 4 3
树呈现一直线,就无法继续更新leaf,wrong了好几次~~~~(>_<)~~~~

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct tree
{int data;
 struct tree *left,*right;
};
int a[10001],b[10001],sum,leaf;
int sort(int kl,int kr,int kL,int kR,struct tree* root)
{struct tree *leftnode,*rightnode;
 int i,pos,l,r,L,R;
 for (i=kR;i>=kL;i--)
 if (a[i]==root->data) {pos=i;break;}

 L=kL; R=pos-1; l=kl; r=kr-(kR-pos)-1;
 if (R>=L)
 {leftnode=(struct tree*)malloc(sizeof(struct tree));
  root->left=leftnode;  leftnode->data=b[r];
  if (L==R) {leftnode->left=NULL;  leftnode->right=NULL;}
       else sort(l,r,L,R,leftnode);
 }
 else root->left=NULL;

 L=pos+1; R=kR; l=kr-(kR-pos); r=kr-1;
 if (R>=L)
 {rightnode=(struct tree*)malloc(sizeof(struct tree));
  root->right=rightnode; rightnode->data=b[r];
  if (L==R) {rightnode->left=NULL; rightnode->right=NULL;}
      else sort(l,r,L,R,rightnode);
 }
 else root->right=NULL;
 return 0;
};
int dfs(struct tree *root,int min)
{
 if ((root->left==NULL)&&(root->right==NULL)&&(sum>min)) {sum=min;leaf=root->data;}
 if (root->left!=NULL) dfs(root->left,min+root->left->data);
 if (root->right!=NULL) dfs(root->right,min+root->right->data);

 return 0;
};
int main()
{int s=1,i;
 struct tree *root;
 char ch;
 while (scanf("%d%c",&a[s],&ch)!=EOF)
 {
  if (ch==' ')
  while (scanf("%d%c",&a[++s],&ch))
  if (ch=='\n') break;
  sum=1;   //SUM=0 树呈一直线就错了,所以赋值大于0
  for (i=1;i<=s;i++)
  {scanf("%d",&b[i]);sum=sum+b[i];}
  root=(struct tree*)malloc(sizeof(struct tree));
  root->data=b[s]; leaf=root->data;
  root->left=NULL; root->right=NULL;
  sort(1,s,1,s,root);
  dfs(root,root->data);
  printf("%d\n",leaf);
  s=1;
 }
 return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值