当前位置:网站首页>计算二叉树的最大路径和
计算二叉树的最大路径和
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;
}
接受建议,如果上述代码不能够找到最大路径和的话,请让我知道二叉树数组。
边栏推荐
- H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
- Natural language processing series (II) -- building character level language model using RNN
- 自然语言处理系列(三)——LSTM
- CDA数据分析——AARRR增长模型的介绍、使用
- (C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
- CDH6之Sqoop添加数据库驱动
- How does Premiere (PR) import the preset mogrt template?
- jenkins 凭证管理
- GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
- 【2022 ACTF-wp】
猜你喜欢
随机推荐
xss-labs-master靶场环境搭建与1-6关解题思路
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
Log4j2
Some problems encountered in introducing lvgl into esp32 Arduino
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
使用Sqoop把ADS层数据导出到MySQL
初始JDBC 编程
Seriation in R: How to Optimally Order Objects in a Data Matrice
Time format display
Leetcode topic [array] -540- single element in an ordered array
Natural language processing series (III) -- LSTM
Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
Leetcode922 按奇偶排序数组 II
字符串回文hash 模板题 O(1)判字符串是否回文
WSL 2 will not be installed yet? It's enough to read this article
uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
Leetcode922 sort array by parity II
Jenkins用户权限管理