题目
47 二叉树中的最大路径和
作者: Turbo时间限制: 1S章节: DS:树
晚于: 2022-08-04 23:55:00后提交分数乘系数50%
问题描述 :
给定一个非空二叉树,返回其最大路径和。
本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。
示例 1:
输入: [1,2,3]
1
/ \
2 3
输出: 6
示例 2:
输入: [-10,9,20,null,null,15,7]
-10
/ \
9 20
/ \
15 7
输出: 42
可使用以下main函数:
#include
#include
#include
#include
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(NULL), right(NULL) {}
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
TreeNode* inputTree()
{
int n,count=0;
char item[100];
cin>>n;
if (n==0)
return NULL;
cin>>item;
TreeNode* root = new TreeNode(atoi(item));
count++;
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (count<n)
{
TreeNode* node = nodeQueue.front();
nodeQueue.pop();
cin>>item;
count++;
if (strcmp(item,"null")!=0)
{
int leftNumber = atoi(item);
node->left = new TreeNode(leftNumber);
nodeQueue.push(node->left);
}
if (count==n)
break;
cin>>item;
count++;
if (strcmp(item,"null")!=0)
{
int rightNumber = atoi(item);
node->right = new TreeNode(rightNumber);
nodeQueue.push(node->right);
}
}
return root;
}
int main()
{
TreeNode* root;
root=inputTree();
int res=Solution().maxPathSum(root);
cout<<res<<endl;
}
输入说明 :
首先输入结点的数目n(注意,这里的结点包括题中的null空结点)
然后输入n个结点的数据,需要填充为空的结点,输入null。
输出说明 :
输出一个整数,表示结果。
输入输出
7
-10 9 20 null null 15 7
42
Code
```cpp
#include <iostream>
#include <queue>
#include <cstdlib>
#include <cstring>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(NULL), right(NULL) {}
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
TreeNode* inputTree()
{
int n,count=0;
char item[100];
cin>>n;
if (n==0)
return NULL;
cin>>item;
TreeNode* root = new TreeNode(atoi(item));
count++;
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (count<n)
{
TreeNode* node = nodeQueue.front();
nodeQueue.pop();
cin>>item;
count++;
if (strcmp(item,"null")!=0)
{
int leftNumber = atoi(item);
node->left = new TreeNode(leftNumber);
nodeQueue.push(node->left);
}
if (count==n)
break;
cin>>item;
count++;
if (strcmp(item,"null")!=0)
{
int rightNumber = atoi(item);
node->right = new TreeNode(rightNumber);
nodeQueue.push(node->right);
}
}
return root;
}
class Solution{
private:
int maxSum=0;
public:
int maxPathSum(TreeNode*root){
maxGain(root );
return maxSum;
}
int maxGain(TreeNode*root ){
if(root== nullptr)
return 0;
//计算左右节点的最大贡献值,只有最大贡献值>0才要
int leftGain=max(maxGain(root->left ),0);
int rightGain=max(maxGain(root->right),0);
if(maxSum< leftGain+rightGain+root->val){
maxSum=leftGain+rightGain+root->val;
}
//返回节点的最大贡献值
return max(leftGain,rightGain)+root->val;
}
// int maxGain(TreeNode*root){
// if(root= nullptr){
// return 0;
// }
// return root->val+ maxGain(root->left), maxGain(root->right ;
// }
};
int main()
{
TreeNode* root;
root=inputTree();
int res=Solution().maxPathSum(root);
cout<<res<<endl;
}