当前位置:网站首页>计算二叉树的最大路径和
计算二叉树的最大路径和
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;
}
接受建议,如果上述代码不能够找到最大路径和的话,请让我知道二叉树数组。
边栏推荐
- 多文件程序X32dbg动态调试
- drools决策表的简单使用
- Leetcode122 the best time to buy and sell stocks II
- Easyexcel and Lombok annotations and commonly used swagger annotations
- Leetcode209 subarray with the smallest length
- Log4j2
- Some problems encountered in introducing lvgl into esp32 Arduino
- 机械臂速成小指南(七):机械臂位姿的描述方法
- 史上最易懂的f-string教程,收藏這一篇就够了
- MSI announced that its motherboard products will cancel all paper accessories
猜你喜欢

H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
![[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)](/img/18/f0c9ef6250a717f8e66c95da4de08c.jpg)
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)

自然语言处理系列(二)——使用RNN搭建字符级语言模型

Flesh-dect (media 2021) -- a viewpoint of material decomposition
![[C language] convert decimal numbers to binary numbers](/img/9b/1848b68b95d98389ed985c83f2e856.png)
[C language] convert decimal numbers to binary numbers

基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)

YYGH-BUG-05

SVO2系列之深度濾波DepthFilter

CDA数据分析——AARRR增长模型的介绍、使用

还不会安装WSL 2?看这一篇文章就够了
随机推荐
Cmake cross compilation
Fabric. JS 3 APIs to set canvas width and height
Natural language processing series (II) -- building character level language model using RNN
JZ63 股票的最大利润
Sort---
Dynamic memory (advanced 4)
使用Sqoop把ADS层数据导出到MySQL
堆(优先级队列)
Test shift left and right
CONDA common command summary
PyTorch搭建LSTM实现服装分类(FashionMNIST)
Easyexcel and Lombok annotations and commonly used swagger annotations
Industry analysis
drools决策表的简单使用
刷题---二叉树--2
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
to_ Bytes and from_ Bytes simple example
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
SSH automatically disconnects (pretends to be dead) after a period of no operation