当前位置:网站首页>Calculate the maximum path sum of binary tree
Calculate the maximum path sum of binary tree
2022-07-02 12:13:00 【weixin_ fifty million one hundred and seventy-nine thousand nin】
No more code, here you are :
// Calculate the maximum sum of a single path
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); // The maximum path and on the left side of the second floor
int MaxValue2 = maxInt(arr, 2, size); // The maximum path and on the right side of the second floor
for (int i = 0; i < size; i++)
{
Value1 = 0;
Value2 = 0;
if (!i)
{
maxValue = maxInt(arr, i, size); // Calculate the maximum path sum under the current node
}
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;
}
Take the advice , If the above code cannot find the maximum path sum , Please let me know binary tree array .
边栏推荐
- [QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
- Depth filter of SvO2 series
- Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
- Take you ten days to easily finish the finale of go micro services (distributed transactions)
- 寻找二叉树中任意两个数的公共祖先
- Post request body content cannot be retrieved repeatedly
- (C language) octal conversion decimal
- PyTorch搭建LSTM实现服装分类(FashionMNIST)
- Deep understanding of NN in pytorch Embedding
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
猜你喜欢

(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?

记录一下MySql update会锁定哪些范围的数据

(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

SVO2系列之深度滤波DepthFilter

Multiply LCA (nearest common ancestor)

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

Performance tuning project case

堆(优先级队列)

初始JDBC 编程

测试左移和右移
随机推荐
drools动态增加、修改、删除规则
Leetcode922 按奇偶排序数组 II
深入理解PyTorch中的nn.Embedding
Time format display
【工控老马】西门子PLC Siemens PLC TCP协议详解
YYGH-BUG-05
Uniapp uni list item @click, uniapp uni list item jump with parameters
(C语言)八进制转换十进制
(C language) octal conversion decimal
drools执行String规则或执行某个规则文件
Jenkins user rights management
Input a three digit number and output its single digit, ten digit and hundred digit.
LeetCode—剑指 Offer 51. 数组中的逆序对
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
CDA数据分析——AARRR增长模型的介绍、使用
PyTorch nn.RNN 参数全解析
JZ63 股票的最大利润
ThreadLocal的简单理解
Gaode map test case
字符串回文hash 模板题 O(1)判字符串是否回文