Question 7 of Group B of the 10th Blue Bridge Cup -- the weight of a complete binary tree (C language)

Question 7 of Group B of the 10th Blue Bridge Cup – The weight of a complete binary tree (C language)

1. Competition topic

1. Topic requirements

Given a complete binary tree containing N nodes, each node in the tree has a weight, and the order from top to bottom and from left to right is A1, A2, AN, as shown in the figure below :
insert image description here
Now Xiao Ming wants to add the weights of nodes at the same depth together. He wants to know which depth has the largest sum of node weights? If the weight sum of multiple depths is the largest, please output the smallest depth among them. Note: The depth of the root is 1.

2. Input and output

Input:
The first line contains an integer N.
The second line contains N integers A1, A2, · · · AN.
Output:
output an integer representing the answer.
Sample input:
7
1 6 5 4 3 2 1
Sample output:
2

2. Analysis process

1. Binary tree property analysis

Binary trees have many important properties. As far as this question is concerned, the properties of binary trees that can be used are as follows:
(1) The maximum number of nodes in a binary tree on layer i is 2 i-1 , i>=1;
(2) A binary tree with a depth of k and 2 k -1 nodes is called a full binary tree;
(3) a binary tree with a depth of k and n nodes, if and only if each of its nodes is related to a depth of k When the nodes numbered from 1 to n correspond one-to-one in a full binary tree, it is called a complete binary tree.
(4) The depth of a complete binary tree with n nodes is [log2 n ]+1;
(5)When the root node of the binary tree starts from 1, when the parent node is i, its left child is 2i, and its right child is 2i+1

2. Practical analysis of binary tree

A complete binary tree like this:
insert image description here

The depth of the complete binary tree Maximum number of nodes per layer
1 20
2 21
3 22
4 23
i 2i -1

3. Key code analysis

So we first need to know the depth of the input binary tree:

while(m)
	{
    
    
		m=m/2;
		k++;
	}
	//求二叉树的深度;

When we know the depth, we can limit the maximum number of nodes in each layer:
When the depth is i, the number of nodes in the i-th layer should be between 2 i-1 and 2 i -1, and to find the i power of 2, we can use the pow(2, i) in the math library function to calculate:

for(int i=1;i<=k;i++)
	{
    
    
		int sum=0;
			//同时本题要求的是哪个深度的节点权值之和最大
			//所以需要一个sum计算和,max来比较;
		for(int j=pow(2,i-1);j<pow(2,i);j++)
			sum+=a[j];
		if(max<sum)
		{
    
    
			max=sum;
			flag=i;
				//记录下最大的结点的深度;
		}
	}

3. Complete code

#include<stdio.h>
#include<math.h>
int a[100010];
int max=-100010;
	//max设置比任何一个结点数都小;
int main()
{
    
    
	int n,k=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
		//输入
	int m=n;
	while(m)
	{
    
    
		m=m/2;
		k++;
	}
		//计算深度;
	int flag=0;
		//深度标记;
	for(int i=1;i<=k;i++)
	{
    
    
		int sum=0;
		for(int j=pow(2,i-1);j<pow(2,i);j++)
			sum+=a[j];
		if(max<sum)
		{
    
    
			max=sum;
			flag=i;
		}
	}
	//主要代码;
	printf("%d",flag);
	return 0;
}

Four. Summary

Let me make a little complaint about how difficult the 9th Blue Bridge Cup is, and I have to make up for the traversal algorithm.

Guess you like

Origin blog.csdn.net/weixin_45843077/article/details/104253910