当前位置:网站首页>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;
}
边栏推荐
- 找合适的PMP机构只需2步搞定,一查二问
- redis02——一篇终结redis的五种数据类型操作命令(可学习、复习、面试、收藏备用)
- How to choose an account opening broker? Is it safe to open an account online?
- Set the icon for the title section of the page
- 11grac turn off archive log
- 设置网页的标题部分的图标
- Build an integrated kubernetes in Fedora
- RAC enable archive log
- The preliminary round of the sixth season of 2022 perfect children's model Foshan competition area came to a successful conclusion
- AWS builds a virtual infrastructure including servers and networks (2)
猜你喜欢

Love analysis released the 2022 love analysis · it operation and maintenance manufacturer panorama report, and an Chao cloud was strongly selected!

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

Why MySQL cannot insert Chinese data in CMD

新唐NUC980使用记录:自制开发板(基于NUC980DK61YC)

利尔达低代码数据大屏,铲平数据应用开发门槛

Unity gets the coordinate point in front of the current object at a certain angle and distance

探讨gis三维系统在矿山行业中的应用

【.NET6】gRPC服务端和客户端开发案例,以及minimal API服务、gRPC服务和传统webapi服务的访问效率大对决
![[learning notes] matroid](/img/e3/4e003f5d89752306ea901c70230deb.png)
[learning notes] matroid

DB
随机推荐
神殿
FFMpeg (一) av_register_all()
【Go ~ 0到1 】 第三天 6月27 slice,map 与 函数
Installing mysql5.7 under Windows
WasmEdge 0.10.0 发布!全新的插件扩展机制、Socket API 增强、LLVM 14 支持
Login common test case
Introduction, compilation, installation and deployment of Doris learning notes
Quelle est la largeur de bande du serveur de bavardage sonore pour des centaines de millions de personnes en même temps?
How do people over 40 allocate annuity insurance? Which product is more suitable?
Two tips for block level elements
887. egg drop
Preparation for Oracle 11g RAC deployment on centos7
Set<String>
ffmpeg推流报错Failed to update header with correct duration.
爱分析发布《2022爱分析 · IT运维厂商全景报告》 安超云强势入选!
Case tool
ROS notes (09) - query and setting of parameters
[learning notes] shortest path + spanning tree
Not so Mobile
JS rounding tips