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


Miguel A. Revilla 
1999-01-11

思路:先建立树,再递归遍历树,求出每一条路径上的结点之和。注意:当左右结点都为空时才对路径之和进行判断。

代码:

#include <iostream>
#include <string>
#include<vector>
#include <sstream>
#include <cstdlib>

using namespace std;

typedef struct tree{
	int value;
	struct tree *lchild,*rchild;
}Tree;

Tree *root=NULL;

int get_pos(vector<int> &inorder,int value,int start,int end)
{//如果没找到返回end
	int i=start;
	for(;i<end;++i){
		if(inorder[i]==value) break;
	}
	return i;
}

Tree *create_node(int value)
{//创建一个结点
	Tree *node=(Tree*) malloc(sizeof(struct tree));
	node->value=value;
	node->lchild=node->rchild=NULL;
	return node;
}

Tree* create_tree(vector<int> &inorder,int in_b,int in_e,vector<int> &postorder,int post_b,int post_e)
{
	if(in_b>=in_e){//判断是否树空
	   return NULL;
	}
	Tree *node=create_node(postorder[post_e-1]);
	if(!root) root=node;
	int _in_e=get_pos(inorder,postorder[post_e-1],in_b,in_e);
	int left_len=_in_e-in_b;
	node->lchild=create_tree(inorder,in_b,_in_e,postorder,post_b,post_b+left_len);//递归创建左子树
	node->rchild=create_tree(inorder,_in_e+1,in_e,postorder,post_b+left_len,post_e-1);//递归创建右子树
	return node;
}

void delete_tree(Tree *t)
{//释放结点
	if(t){
		delete_tree(t->lchild);
		delete_tree(t->rchild);
	    free(t);
		t=NULL;
	}
}

void print(Tree *t)
{//测试用
	if(t){
		cout<<t->value<<" ";
		print(t->lchild);
		print(t->rchild);
	}
}

void find_least_sum(Tree *t,bool &is_null ,int &sum,int &leaf,int temp_sum,int temp_leaf)
{//求最小和以及最小和对应的最小叶子结点
	if(t){
       temp_sum+=t->value;
	   temp_leaf=t->value;
	   bool left_null=false,right_null=false;
	   find_least_sum(t->lchild,left_null,sum,leaf,temp_sum,t->value);
	   find_least_sum(t->rchild,right_null,sum,leaf,temp_sum,t->value);
	   if(left_null && right_null){//左右子树空
		   if(sum==0){
			   sum=temp_sum;
			   leaf=temp_leaf;
		   }else{
			   if(temp_sum<sum){
				   sum=temp_sum;
				   leaf=temp_leaf;
			   }else if(temp_sum==sum){
				   if(temp_leaf<leaf) leaf=temp_leaf;
			   }
		   }
	   }
	}else{
		is_null=true;
	}
}

int main()
{
    string line1,line2;
	while(getline(cin,line1) && getline(cin,line2)){//先以字符串形式读入一整行
		istringstream istream1(line1);
		istringstream istream2(line2);
		int _value;
		vector<int> v1,v2;
		while(istream1>>_value){
			v1.push_back(_value);
		}
		while(istream2>>_value){
			v2.push_back(_value);
		}
		root=NULL;
		create_tree(v1,0,v1.size(),v2,0,v2.size());
	//	print(root);
	    int sum=0,leaf=0;
		bool is_null=false;
		find_least_sum(root,is_null,sum,leaf,0,0);
		delete_tree(root);
		cout<<leaf<<endl;
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值