UVA 297 Tree

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=43041#problem/K

题目大意:给一个树的中序遍历和后序遍历,求出它的根到叶子的节点值的最短值取到时的叶子结点,当最短值可以取到多次时,取叶子结点最小的那一个。

题目思路:根据中序遍历和后序遍历先递归构造树,后爆搜一下就好

题目坑点:无(如果输入时不告诉你结点个数不算的话)

代码如下:

#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int类型最大值INT_MAX,short最大值为SHORT_MAX
long long最大值为LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
int in[10010],post[10010];
int n,root,key,cnt,res,min_path,min_leaf;
struct tree
{
	int lef;
	int rig;
	int num;
}node[10010];
void init(string s)
{
    for(int i=0;s[i];i++)
    {
        while(s[i]==' ')
        i++;
        in[n]=0;
        while(s[i]&&isdigit(s[i]))
        {
            in[n]=in[n]*10+s[i]-'0';
            i++;
        }
        n++;
        if(!s[i]) break;
    }
}
int build(int head1,int tail1,int head2,int tail2)
{
	int x = cnt,i;
	cnt++;
	for(i = head1;i<=tail1;i++)
	if(post[tail2] == in[i]) break;
	node[x].num = post[tail2];
	if(head1 == tail1) return x;
	
	if(i>head1)
	node[x].lef = build(head1,i-1,head2,head2+i-head1-1);
	if(i<tail1)
	node[x].rig = build(i+1,tail1,head2+i-head1,tail2-1);
	
	//cout << x << " " << node[x].lef << " " << node[x].rig << endl;
	//cout << node[x].num << " " << node[node[x].lef].num << " " << node[node[x].rig].num << endl;
	return x;
}
void dfs(int tot,int tri)
{
	if(node[tri].lef == 0 && node[tri].rig == 0)
	{
		//cout << "ok" << endl;
		if(tot+node[tri].num==min_path)
		min_leaf = min(min_leaf,node[tri].num);
		if(tot+node[tri].num<min_path)
		{
			min_path = tot+node[tri].num;
			min_leaf = node[tri].num;
		}
	}
	if(node[tri].lef != 0) dfs(tot+node[tri].num,node[tri].lef);
	if(node[tri].rig != 0) dfs(tot+node[tri].num,node[tri].rig);
}
int main()
{
    //freopen("sample.in", "r", stdin);
	//freopen("sample.out", "w", stdout);
	
	string s;
	while(getline(cin,s))
	{
		_clr(node);
		n = 0;
		min_path = INT_MAX;
		min_leaf = INT_MAX;
		cnt = 1;
		init(s);
		for(int i = 0;i<n;i++) cin >> post[i];
		build(0,n-1,0,n-1);
		dfs(0,1);
		cout << min_leaf << endl;
		//cout << cnt << endl;
		//cout << node[1].num << " " << node[1].lef << " " << node[1].rig << endl;
		cin.ignore(100,'\n');
	}

    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值