UVA699-落叶-二叉树

1:用一根数轴,根结点是坐标index,左结点-1,右结点+1

还有一个0ms的不知道怎么过的

#include<stdio.h>
#include<iostream>
#include <strstream>
#include<string>
#include<memory.h>
#include<sstream>
using namespace std;
void caculate(int* tree, int index, int* min, int*max)
{
	int s;
	cin >> s;
	if (s != -1)
	{
		tree[index - 1] += s;
		if ((index - 1) < *min)
		{
			*min = index-1;
		}
		caculate(tree, index - 1, min, max);
	}
	cin >> s;
	if (s != -1)
	{
		tree[index + 1] += s;
		if ((index +1) > *max)
			{
				*max = index+1;
			}
		caculate(tree, index + 1, min, max);
	}
}
int main()
{
	freopen("d:\1.txt", "r", stdin);
	int MAXN = 10000;
	int number = 1;
	while (cin)
	{
		int MIDDLE = 5000;
		int total[MAXN];
		memset(total, 0, sizeof(int) * MAXN);
		int s;
		cin >> s;
		if (s == -1)
			return 0;
		else
		{
			int max = MIDDLE;
			int min = MIDDLE;
			total[MIDDLE] += s;
			caculate(total, MIDDLE, &min, &max);
			cout << "Case " << number << ":" << endl;
			for (int i = min; i <= max; i++)
				if (i == min)
					cout << total[i];
				else
					cout << " " << total[i];
			cout << endl<<endl;
		}
		number++;
	}
}

  

原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/6836673.html