1. 程式人生 > >699 The Falling Leaves

699 The Falling Leaves

Description

Download as PDF

Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large would the piles of leaves become?


We assume each node in a binary tree "drops" a number of leaves equal to the integer value stored in that node. We also assume that these leaves drop vertically to the ground (thankfully, there's no wind to blow them around). Finally, we assume that the nodes are positioned horizontally in such a manner that the left and right children of a node are exactly one unit to the left and one unit to the right, respectively, of their parent. Consider the following tree:

The nodes containing 5 and 6 have the same horizontal position (with different vertical positions, of course). The node containing 7 is one unit to the left of those containing 5 and 6, and the node containing 3 is one unit to their right. When the "leaves" drop from these nodes, three piles are created: the leftmost one contains 7 leaves (from the leftmost node), the next contains 11 (from the nodes containing 5 and 6), and the rightmost pile contains 3. (While it is true that only leaf nodes in a tree would logically have leaves, we ignore that in this problem.)

Input 

The input contains multiple test cases, each describing a single tree. A tree is specified by giving the value in the root node, followed by the description of the left subtree, and then the description of the right subtree. If a subtree is empty, the value -1 is supplied. Thus the tree shown above is specified as 5 7 -1 6 -1 -1 3 -1 -1
. Each actual tree node contains a positive, non-zero value. The last test case is followed by a single -1 (which would otherwise represent an empty tree). For each test case, display the case number (they are numbered sequentially, starting with 1) on a line by itself. On the next line display the number of "leaves" in each pile, from left to right, with a single space separating each value. This display must start in column 1, and will not exceed the width of an 80-character line. Follow the output for each case by a blank line. This format is illustrated in the examples below.
5 7 -1 6 -1 -1 3 -1 -1
8 2 9 -1 -1 6 5 -1 -1 12 -1
-1 3 7 -1 -1 -1
-1
Case 1:
7 11 3

Case 2:
9 7 21 15

分析:先序遍歷邊讀邊算。

#include <iostream>
#include <cstring>

using namespace std;
const int maxn = 84;
int sum[maxn];

void build(int p){
	int v;
	cin >> v;
	if(v==-1)return ;
	sum[p] += v;
	build(p-1); build(p+1);
}

bool init(){
	int v;
	cin >> v;
	if(v==-1)return false;
	int p = maxn/2;
	memset(sum, 0, sizeof(sum));
	sum[p] = v;
	build(p-1); build(p+1);
}

int main()
{
	int kase = 0;
	while(init()){
		int p = 0;
		while(sum[p]==0)p++;
		cout << "Case " << ++kase << ":\n" << sum[p++];
		while(sum[p]!=0)cout << " " << sum[p++];
		cout << "\n\n";
	}
    return 0;
}


相關推薦

UVa 699 The Falling Leaves(遞歸建樹)

page 得到 break case 也會 slist 註意 stream pan UVa 699 The Falling Leaves(遞歸建樹)   假設一棵二叉樹也會落葉 而且葉子只會垂直下落 每個節點保存的值為那個節點上的葉子數 求所有葉子全部下落後

UVA-699 The Falling Leaves

對於這個題目,要分析學習的還是所應用的遞迴。 在寫遞迴具體步驟的時候,可以把目光集中到遞迴當中的一 項上,比如遞迴剛開始怎麼走,遞迴最後一步要結束了該怎麼走。 想遞迴,要著眼於大局,寫遞迴,要工筆細描,著眼單步; 這道題目是應用於樹的先序方式輸入的遞迴方式,在ini

699 The Falling Leaves

Description Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quic

二叉樹的遞歸遍歷 The Falling Leaves UVa 699

輸入 out col ges while com cst bsp images 題意:對於每一棵樹,每一個結點都有它的水平位置,左子結點在根節點的水平位置-1,右子節點在根節點的位置+1,從左至右輸出每個水平位置的節點之和 解題思路:由於上題所示的遍歷方式如同二叉樹的前序

下落的樹葉 (The Falling Leaves UVA - 699

sum std name fall include ++ 源代碼 lock 水平 題目描述: 原題:https://vjudge.net/problem/UVA-699 題目思路: 1.依舊二叉樹的DFS 2.建樹過程中開個數組統計 //紫書源代碼WA

The Falling Leaves UVA - 699

子節點 ase cin fall tdi mes 所有 out eof 題目鏈接:https://vjudge.net/problem/UVA-699 題目大意:給一顆二叉樹,每個結點都有一個水平位置 :左子節點在它左邊的1個單位,右子結點在它右邊1個單位。從左

【數據結構】The Falling Leaves(6-10)

stream char cas string ons cstring con bsp sta [UVA699]The Falling Leaves 算法入門經典第6章例題6-10(P159) 題目大意:有一顆二叉樹,求水平位置的和。 試題分析:亂搞就可以過,將樹根節點

UVA699 The Falling Leaves 遞迴建樹

題意翻譯 每年秋天,在中北部地區葉子的顏色都會變得鮮豔,樹葉也迅速地跟著落下。如果同樣的事情發生了在二叉樹上,那麼這些樹葉堆有多大? 我們假設一個二叉樹中的每個節點都會在那個節點落下一個等於整數值的葉子數。 我們還假設這些葉子垂直地落在地上(謝天謝地,沒有風吹過他們周圍)。

Falling Leaves(10.1.2))

Figure 1 Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a bin

poj1577 Falling Leaves 二叉排序樹

  Figure 1 Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a bi

POJ 1577 Falling Leaves

hat trees example ++ prope 需要 ngs emp space Description Figure 1Figure 1 shows a graphical representation of a binary tree of letters. P

kali 2016:mount ntfs 分區只讀 --Falling back to read-only mount because the NTFS partition is in an unsafe state.

start ack umount part fas mnt fast pan fsp mount ntfs 分區 mount /dev/sdb1 /mnt/d 提示: The disk contains an unclean file system (0, 0).Metad

699. Falling Squares

On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th square dropped (positions[i] = (left, side_length))

The reasons for bitcoin’s falling

Over time, cryptocurrencies seem to be getting more and more, causing many investors to panic. The encryption market situation throughou

Why is the advantage of Bitcoin falling?

Bitcoin accounted for the total market capitalization of the cryptocurrency market. After reaching the high point of the beginning of the

Matter falling into a black hole at 30 percent of the speed of light

Black holes are objects with such strong gravitational fields that not even light travels quickly enough to escape their grasp, hence the description 'bla

[CSS] Showing horizontal scrollbar always for the table

nbsp overflow tab spl spa horizon clas pre idt table { display: block; overflow: scroll; width: 200px; height:95vh;

[LeetCode] Reshape the Matrix 矩陣重塑

ren ati num 我們 資料 call posit tar led In MATLAB, there is a very useful function called ‘reshape‘, which can reshape a matrix into a ne

ZOJ - 3228 Searching the String (AC自己主動機)

won key wid roc lap processor som multiple pla Description Little jay really hates to deal with string. But moondy likes it very mu

SQL Server導入報錯:The LocaleID 4 is not installed on this system

code page localeid 936 問題描述:通過SQLServer導入導出向導導入中文字符集數據,遇到The LocaleID 4 is not installed on this system.錯誤。解決方法:我發現客戶服務器操作系統雖然不支持Chinese (Simplified)