当前位置:网站首页>计算二叉树的最大路径和
计算二叉树的最大路径和
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;
}
接受建议,如果上述代码不能够找到最大路径和的话,请让我知道二叉树数组。
边栏推荐
- Leetcode739 每日温度
- 甜心教主:王心凌
- 全链路压测
- GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
- HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
- 字符串回文hash 模板题 O(1)判字符串是否回文
- K-Means Clustering Visualization in R: Step By Step Guide
- 多文件程序X32dbg动态调试
- 5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
猜你喜欢

堆(優先級隊列)

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

HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R

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

Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)

Test shift left and right

File operation (detailed!)

ThreadLocal的简单理解

Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law

YYGH-BUG-04
随机推荐
Dynamic memory (advanced 4)
Jenkins user rights management
Larvel modify table fields
lombok常用注解
ORB-SLAM2不同线程间的数据共享与传递
to_ Bytes and from_ Bytes simple example
ThreadLocal的简单理解
Mish-撼动深度学习ReLU激活函数的新继任者
Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
Test shift left and right
Maximum profit of jz63 shares
The most understandable f-string tutorial in history, collecting this one is enough
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
Sort---
【C语言】十进制数转换成二进制数
WSL 2 will not be installed yet? It's enough to read this article
PyTorch nn. Full analysis of RNN parameters
Deep understanding of P-R curve, ROC and AUC
QT meter custom control
Leetcode14 最长公共前缀