UVA - 548 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

题意:

二叉树的遍历,给出中序遍历和后序遍历,要求出所有的结点和最小的时候的那个结点。

后序遍历可以知道,最后一位数字就是根结点,根据这一个可以知道中序遍历的根结点的位置,从而它的左边为左子树,右边即为右子树;


#include<iostream>
#include<cctype>
#include<cstring>
#include<cstdio>
#include<algorithm>
const int maxn = 10010;
using namespace std;
char str1[maxn*10];
char str2[maxn*10];
int arr1[maxn];
int arr2[maxn];
int ans;
int MIN ;
int count1 ;
int count2 ;
struct node
{
	int date;
	node *left;
	node *right;

};
void init() {
	memset(arr1,0,sizeof(arr1));
	memset(arr2,0,sizeof(arr2));
	count1 = 0;
	count2 = 0;
	ans = 0;
	MIN = 999999;
	return ;

}

void input()
{
	int len1 = strlen(str1);
	int len2 = strlen(str2);
	for(int i = 0; i < len1;)
	{   if(isdigit(str1[i]))
		{
			int num = 0 ;
			while(isdigit(str1[i]))
			{
				num = num*10 + str1[i] - '0';
				i ++;
			}
			arr1[count1] = num ;
			count1 ++ ;
		}
		else
			i++;	
	}

	for(int i = 0; i < len2;)
	{

		if(isdigit(str2[i]))
		{
			int num = 0;
			while(isdigit(str2[i]))
			{
				num = num * 10 + str2[i] - '0';
				i++;
			}


			arr2[count2] = num ;
			count2 ++ ;
		}
		else
			i++;

	}

	return ;

}

node *creat(int a)
{
	node *no = new node;
	no->date = a;
	no->left = NULL;
	no->right = NULL;
	return no;
}

node *build(node *root ,int a[],int b[],int ab,int ae,int bb,int be)
{
	if(bb <= be)
	{
		node *no = creat(b[be]);
		root = no;
		int wz = 0;
		for(int i = ab; i<= ae ; i++)
		{
			if(a[i] == b[be])
			{
				wz = i;
				break;
			}

		}

		root->left = build(root->left,a,b,ab,wz-1,bb,bb + wz -1 -ab);
		root->right = build(root->right,a,b,wz + 1,ae,bb + wz -ab,be-1);

		return root;

	}
	else
		return NULL;

}

void dfs(node *u,int cur)
{
	if(u)
	{
		cur += u->date;
		if(!u->left && !u->right)
		{
			if(cur < MIN)
			{
				MIN = cur;
				ans = u->date ;

			}
			return ;	
		}
		if(u->left) dfs(u->left,cur);
		if(u->right) dfs(u->right,cur);
	}
	else
		return ;

}

int main()
{
	while(gets(str1))
	{
		gets(str2);
		init();
		input ();
		node *root = NULL;
		root = build(root,arr1,arr2,0,count1-1,0,count2 -1);
		dfs(root,0);
		cout<<ans<<endl;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值