当前位置:网站首页>The Falling Leaves
The Falling Leaves
2022-06-28 08:31:00 【Angeliaaa】
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 on the right: 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).
Output
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 theexamples below.
Sample Input
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
Sample Output
Case 1:
7 11 3
Case 2:
9 7 21 15
The question : Given a binary tree , Each node has a balanced position , The left child node is a unit to his left , The right child node is a unit to his right , requirement Output the sum of the weights of all nodes at each balance position from left to right .
Ideas : What is given in the question is the first order convenience , Read data directly and recursively , Simulate update while reading in , Find the equilibrium position of the tree root . Just control the output , The code is as follows :
#include<stdio.h>
#include<string.h>
int sum[10010];
void build(int p, int num)
{
sum[p] += num;
int v;
scanf("%d", &v);
if (v!=-1)
build(p-1,v);
scanf("%d",&v);
if (v!=-1)
build(p+1,v);
return ;
}
int main()
{
int T, i, k = 1;
while(~scanf("%d",&T)&&T!=-1)
{
memset(sum,0,sizeof(sum));
printf("Case %d:\n", k++);
build(500,T);
int flag = 0;
for(i=0;i<1000;i++)
{
if(sum[i]!= 0)
{
if(!flag)
{
printf("%d",sum[i]);
flag = 1;
}
else printf(" %d",sum[i]);
}
}
printf("\n\n");
}
return 0;
}
边栏推荐
- B_ QuRT_ User_ Guide(29)
- yaml json
- Case tool
- 2022第六季完美童模 佛山赛区 初赛圆满落幕
- CloudCompare&PCL 点云SVD分解
- Unity gets the coordinate point in front of the current object at a certain angle and distance
- Map. ToCharArray( ),Map. getOrDefault(). Map. charAt()
- Login common test case
- Build an integrated kubernetes in Fedora
- Oracle RAC -- understanding of VIP
猜你喜欢
![DELL R730服务器开机报错:[XXX] usb 1-1-port4: disabled by hub (EMI?), re-enabling...](/img/90/425965ca4b3df3656ce2a5f4230c4b.jpg)
DELL R730服务器开机报错:[XXX] usb 1-1-port4: disabled by hub (EMI?), re-enabling...

第六届智能家居亚洲峰会暨精品展(Smart Home Asia 2022)将于10月在沪召开

B_ QuRT_ User_ Guide(28)

Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week

匿名页的反向映射

AI chief architect 8-aica-gao Xiang, in-depth understanding and practice of propeller 2.0

PLSQL installation under Windows
![Dell r730 server startup error: [xxx] USB 1-1-port4: disabled by hub (EMI?), re-enabling...](/img/90/425965ca4b3df3656ce2a5f4230c4b.jpg)
Dell r730 server startup error: [xxx] USB 1-1-port4: disabled by hub (EMI?), re-enabling...

Solution: selenium common. exceptions. WebDriverException: Message: ‘chromedriver‘ execu

叠加阶梯图和线图及合并线图和针状图
随机推荐
AWS saves data on the cloud (3)
神殿
第六届智能家居亚洲峰会暨精品展(Smart Home Asia 2022)将于10月在沪召开
Force buckle 1884 Egg drop - two eggs
Understanding of CUDA, cudnn and tensorrt
AWS builds a virtual infrastructure including servers and networks (2)
Priority of JS operator
[learning notes] simulation
新唐NUC980使用记录:自制开发板(基于NUC980DK61YC)
开户券商怎么选择?网上开户是否安全么?
Robot Rapping Results Report
Usage record of Xintang nuc980: self made development board (based on nuc980dk61yc)
【学习笔记】搜索
小艺人黄鑫洋受邀参加巴黎时装周儿童单元武汉站
A - 深海探险
webrtc优势与模块拆分
Solve NPM err! Unexpected end of JSON input while parsing near
Oracle view tablespace usage
Why are function templates not partial specialization?
The maximum number of Rac open file descriptors, and the processing of hard check failure