当前位置:网站首页>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 .
边栏推荐
- ORB-SLAM2不同线程间的数据共享与传递
- CDH6之Sqoop添加数据库驱动
- CDA数据分析——AARRR增长模型的介绍、使用
- 记录一下MySql update会锁定哪些范围的数据
- Jenkins user rights management
- 基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
- 堆(优先级队列)
- Brush questions --- binary tree --2
- Tas (file d'attente prioritaire)
- Mish shake the new successor of the deep learning relu activation function
猜你喜欢
初始JDBC 编程
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
WSL 2 will not be installed yet? It's enough to read this article
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
自然语言处理系列(二)——使用RNN搭建字符级语言模型
Jenkins voucher management
Mysql database foundation
MySQL与PostgreSQL抓取慢sql的方法
寻找二叉树中任意两个数的公共祖先
Addition, deletion, modification and query of MySQL table (Advanced)
随机推荐
输入一个三位的数字,输出它的个位数,十位数、百位数。
Leetcode14 longest public prefix
This article takes you to understand the operation of vim
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
Map and set
PyTorch搭建LSTM实现服装分类(FashionMNIST)
(C语言)八进制转换十进制
Applet link generation
Lombok common annotations
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
Orb-slam2 data sharing and transmission between different threads
Intel 内部指令 --- AVX和AVX2学习笔记
Filtre de profondeur de la série svo2
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
Uniapp uni list item @click, uniapp uni list item jump with parameters
The most understandable f-string tutorial in history, collecting this one is enough
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
LeetCode—剑指 Offer 59 - I、59 - II
堆(优先级队列)