二叉树算法题—— [蓝桥杯 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;
}

测评结果:

相关推荐
念恒123067 分钟前
继承(下) (Inheritance)
c++
海清河晏1111 小时前
数据结构 | 单循环链表
数据结构·算法·链表
H Journey1 小时前
C++之 CMake、CMakeLists.txt、Makefile
开发语言·c++·makefile·cmake
wuweijianlove5 小时前
算法性能的渐近与非渐近行为对比的技术4
算法
研究点啥好呢5 小时前
Github热门项目推荐 | 创建你的像素风格!
c++·python·node.js·github·开源软件
_dindong5 小时前
cf1091div2 C.Grid Covering(数论)
c++·算法
AI成长日志5 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
沫璃染墨6 小时前
C++ string 从入门到精通:构造、迭代器、容量接口全解析
c语言·开发语言·c++
黎阳之光6 小时前
黎阳之光:视频孪生领跑者,铸就中国数字科技全球竞争力
大数据·人工智能·算法·安全·数字孪生
skywalker_116 小时前
力扣hot100-3(最长连续序列),4(移动零)
数据结构·算法·leetcode