H - 从中序和后续恢复二叉树

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<algorithm>
#include<string>
#include<vector>
#include<sstream>
#include<cstring>
using namespace std;
typedef struct tree{
	int data;
	tree* l;
	tree* r;
}tree;
const int maxn=0x3f3f3f3f;
int sub(vector<int> &a,int origin,int end,vector<int> &b)
{
	int i,k=0;
	for(i=origin;i<=end;i++)
	{k++;b.push_back(a[i]);}
	return k;
}
int ans=maxn,leaf=maxn;
tree* creat(vector<int> &z,vector<int> &h,int n,int sum)
{
	if(sum>ans) return NULL;
	if(n==0) return NULL;
	tree *t;
	t=(tree*)malloc(sizeof(tree));
	vector<int> lz,lh,rz,rh;int i;
	for(i=0;i<n;i++)
	if(z[i]==h[n-1])
	{break;}
	int len_lz,len_rz;
	len_lz=sub(z,0,i-1,lz);len_rz=sub(z,i+1,n-1,rz);
	sub(h,0,len_lz-1,lh);sub(h,len_lz,n-2,rh);
	if(n==1) 
	{
		(*t).data=h[n-1];sum=sum+h[n-1];
		(*t).l=NULL;(*t).r=NULL;
		if(sum<ans) {ans=sum;leaf=h[n-1];}
		else if(sum==ans&&h[n-1]<leaf) {leaf=h[n-1];}
	}
	else
	{
		(*t).data=h[n-1];
		sum+=h[n-1];
		(*t).l=creat(lz,lh,len_lz,sum);
		(*t).r=creat(rz,rh,len_rz,sum);
	}
	return t;
}
void remove_tree(tree* root)
{
	if(root!=NULL)
	{
		remove_tree(root->l);
		remove_tree(root->r);
		free(root);
	}
}
int main()
{
	vector<int> z,h;
	string h1,z1;
	while(getline(cin,z1))
	{
		leaf=maxn;ans=maxn;
		getline(cin,h1);
		int i=0,j=0,n=0,a,b;
		stringstream ss(z1),ss1(h1);
		while(ss>>a&&ss1>>b)
		{
			z.push_back(a);
			h.push_back(b);
			i++;n++;
		}
		tree* root;
		root=creat(z,h,n,0);
		printf("%d\n",leaf);
		z.clear();h.clear();
		remove_tree(root);
	}
	return 0;
}

题目大意:

    输入一个二叉树的中序和后序,输出一个叶子节点,该叶子节点到根的数值总和最小。

解题思路:

    先通过后序和中序建立二叉树,在建树的过程中直接寻找路径值最小的叶节点。

这一题错了n次(泪流满面),先是stringsrteam用重复导致时间超限,后来因为数组大小问题导致错误,改用vector,接着题目中maxn的值一开始定义10000过小,又是WA,改过来后终于正确,路途坎坷。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值