二叉树算法题—— [蓝桥杯 2019 省 AB] 完全二叉树的权值

题目https://www.luogu.com.cn/problem/P8681

代码如下:

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;
#define fs first
#define sc second
#define endl '\n'
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> PII;

const int N = 1e5;

int minh = -1;

int n;

int sum[N],a[N];

void dfs(int node,int h){
	if(node>n)return;
	
	sum[h]+=a[node];
	
	dfs(2*node,h+1);
	dfs(2*node+1,h+1);

    if(sum[h]>sum[minh]) minh = h;
}

int main(){
	
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    cin>>n;
    
    for(int i=1;i<=n;i++)cin>>a[i];
    
    dfs(1,1);
    
    cout<<minh<<endl;
    
    return 0;
}

测评结果:

相关推荐
不正经学生2 小时前
C语言预处理详解:编译器真正动手之前的那些事
c语言·开发语言·算法·面试·bug
毕竟是shy哥5 小时前
计算YOLO数据集中每个类的目标数
算法·yolo·机器学习
M78佐菲5 小时前
Linux学习笔记:TCP协议
linux·笔记·学习·tcp/ip·算法
恋恋西风5 小时前
C++ 理解 std::thread 在单核和多核上的行为差异
开发语言·c++
Brilliantwxx5 小时前
【Linux】 进程(9)程序与进程地址空间(基础+进阶+面试题)
linux·运维·服务器·开发语言·c++
BizzZ_6 小时前
C++(22)——类型转换和IO流
开发语言·c++
晊晌_h6 小时前
嵌入式从0到精通——数据结构总结[特殊字符]
数据结构·算法·排序算法
我找到地球的支点啦7 小时前
Matlab系列(009) 一CRC循环冗余校验详解
开发语言·数据结构·算法·matlab·信息与通信
罗西的思考7 小时前
【OpenClaw具身硬件】ZeroClaw 源码阅读笔记(3)--- RAG
人工智能·算法·机器学习
浪里镖客8 小时前
位姿转换矩阵写法-个人习惯(计算机理解其实是相反的)
线性代数·算法·矩阵