当前位置:网站首页>计算二叉树的最大路径和
计算二叉树的最大路径和
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;
}
接受建议,如果上述代码不能够找到最大路径和的话,请让我知道二叉树数组。
边栏推荐
- [geek challenge 2019] upload
- PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
- 高德地图测试用例
- Log4j2
- Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
- lombok常用注解
- Fabric. JS 3 APIs to set canvas width and height
- H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
- 机械臂速成小指南(七):机械臂位姿的描述方法
- 求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
猜你喜欢

Dynamic debugging of multi file program x32dbg

Map和Set

【工控老马】西门子PLC Siemens PLC TCP协议详解

堆(优先级队列)

H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser

Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator

Differences between nodes and sharding in ES cluster

Jenkins voucher management

PyTorch搭建LSTM实现服装分类(FashionMNIST)

Natural language processing series (I) -- RNN Foundation
随机推荐
Natural language processing series (II) -- building character level language model using RNN
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
How does Premiere (PR) import the preset mogrt template?
SCM power supply
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
CONDA common command summary
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
HOW TO ADD P-VALUES TO GGPLOT FACETS
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
PyTorch中repeat、tile与repeat_interleave的区别
MSI announced that its motherboard products will cancel all paper accessories
Leetcode922 按奇偶排序数组 II
Leetcode739 daily temperature
Pytorch builds LSTM to realize clothing classification (fashionmnist)
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
[C language] Yang Hui triangle, customize the number of lines of the triangle
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
使用Sqoop把ADS层数据导出到MySQL
Industry analysis
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开