当前位置:网站首页>计算二叉树的最大路径和
计算二叉树的最大路径和
2022-07-02 09:43:00 【weixin_50179990】
不多说代码奉上:
//计算单条路径的最大和
int maxInt(int arr[], int index, int size)
{
int index1 = index * 2 + 1;
int index2 = index * 2 + 2;
if (index1 >= size || index2 >= size)
{
return arr[index];
}
int value1 = maxInt(arr, index1, size);
int value2 = maxInt(arr, index2, size);
if (value1 > value2)
{
return arr[index] + value1;
}
else
{
return arr[index] + value2;
}
}
//
int MaxLengthTwoTree(int arr[],int size)
{
int *a = new int[size];
int maxValue = 0;
int Value1 = 0;
int Value2 = 0;
int ind = 0;
int MaxValue1 = maxInt(arr, 1, size); //第二层左侧的最大路径和
int MaxValue2 = maxInt(arr, 2, size); //第二层右侧的最大路径和
for (int i = 0; i < size; i++)
{
Value1 = 0;
Value2 = 0;
if (!i)
{
maxValue = maxInt(arr, i, size); //计算当前节点之下的最大路径和
}
else
{
if (i*2 + 1 <= size && i * 2 + 2 <= size)
{
Value1 += maxInt(arr, i * 2 + 1, size);
Value1 += maxInt(arr, i * 2 + 2, size);
Value1 += arr[i];
}
else
{
Value1 += arr[i];
}
int indexI = i;
while (indexI >= 3)
{
if (indexI % 2 == 1)
{
Value2 += arr[indexI];
indexI = (indexI - 1) / 2;
}
else
{
Value2 += arr[indexI];
indexI = (indexI - 2) / 2;
}
}
if (indexI == 1)
{
Value2 += arr[indexI] + MaxValue2 + arr[0];
}
else if (indexI == 2)
{
Value2 += arr[indexI] + MaxValue1 + arr[0];
}
if (Value1 > Value2)
{
if (Value1 > maxValue)
{
maxValue = Value1;
}
}
else
{
if (Value2 > maxValue)
{
maxValue = Value2;
}
}
}
}
return maxValue;
}
接受建议,如果上述代码不能够找到最大路径和的话,请让我知道二叉树数组。
边栏推荐
- HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
- SVO2系列之深度濾波DepthFilter
- Dynamic memory (advanced 4)
- JZ63 股票的最大利润
- 【2022 ACTF-wp】
- Leetcode14 longest public prefix
- Leetcode topic [array] -540- single element in an ordered array
- Input a three digit number and output its single digit, ten digit and hundred digit.
- XSS labs master shooting range environment construction and 1-6 problem solving ideas
- Deep understanding of NN in pytorch Embedding
猜你喜欢

Flesh-dect (media 2021) -- a viewpoint of material decomposition

H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开

Mish shake the new successor of the deep learning relu activation function

GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
![[C language] convert decimal numbers to binary numbers](/img/9b/1848b68b95d98389ed985c83f2e856.png)
[C language] convert decimal numbers to binary numbers

Map和Set

多文件程序X32dbg动态调试

CDH6之Sqoop添加数据库驱动

还不会安装WSL 2?看这一篇文章就够了

Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
随机推荐
Yygh-9-make an appointment to place an order
CDA数据分析——AARRR增长模型的介绍、使用
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
Implementation of address book (file version)
机械臂速成小指南(七):机械臂位姿的描述方法
PyTorch nn. Full analysis of RNN parameters
自然语言处理系列(二)——使用RNN搭建字符级语言模型
YYGH-BUG-04
PyTorch中repeat、tile与repeat_interleave的区别
[geek challenge 2019] upload
Read the Flink source code and join Alibaba cloud Flink group..
自然语言处理系列(一)——RNN基础
【C语言】十进制数转换成二进制数
[C language] convert decimal numbers to binary numbers
浅谈sklearn中的数据预处理
子线程获取Request
二分刷题记录(洛谷题单)区间的甄别
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
drools决策表的简单使用
Time format display