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出最价值最小的路径,输出的是最小路径的叶子节点;
首先我们来看下面的二叉树:
![用中序遍历和后序遍历创建二叉树的思想方法 - 清培 - 清培的博客](http://img.ph.126.net/miqCuPQZjcBiRPWRsti88Q==/937874622400503725.jpg)
b[ ] l r 7 4 5 2 6 3 1
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
6 3 lr[5,6] l=kr-(kR-pos); r=kr-1;
当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
#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;
}