当前位置:网站首页>计算二叉树的最大路径和
计算二叉树的最大路径和
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;
}
接受建议,如果上述代码不能够找到最大路径和的话,请让我知道二叉树数组。
边栏推荐
- GGPlot Examples Best Reference
- 高德地图测试用例
- 深入理解P-R曲线、ROC与AUC
- (C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
- Natural language processing series (II) -- building character level language model using RNN
- 多文件程序X32dbg动态调试
- Log4j2
- 【工控老马】西门子PLC Siemens PLC TCP协议详解
- 排序---
- Applet link generation
猜你喜欢

Tas (file d'attente prioritaire)

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

How to Easily Create Barplots with Error Bars in R

CDA data analysis -- Introduction and use of aarrr growth model

Dynamic debugging of multi file program x32dbg

How does Premiere (PR) import the preset mogrt template?

Depth filter of SvO2 series

自然语言处理系列(一)——RNN基础

BEAUTIFUL GGPLOT VENN DIAGRAM WITH R

CONDA common command summary
随机推荐
How to Add P-Values onto Horizontal GGPLOTS
Mish-撼动深度学习ReLU激活函数的新继任者
还不会安装WSL 2?看这一篇文章就够了
CDA数据分析——AARRR增长模型的介绍、使用
Cmake cross compilation
甜心教主:王心凌
SVO2系列之深度濾波DepthFilter
【工控老马】西门子PLC Siemens PLC TCP协议详解
Those logs in MySQL
lombok常用注解
多文件程序X32dbg动态调试
uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
Larvel modify table fields
Orb-slam2 data sharing and transmission between different threads
二分刷题记录(洛谷题单)区间的甄别
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
Leetcode739 daily temperature
Codeforces 771-div2 C (trouble, permutation is not very good)
字符串回文hash 模板题 O(1)判字符串是否回文
MSI announced that its motherboard products will cancel all paper accessories