topic
47 Maximum path sum in binary tree
Author: Turbo Time Limit: 1S Chapter: DS:Tree
Submit after: 2022-08-04 23:55:00 and the score multiplier will be 50%
Problem Description :
Given a non-empty binary tree, return its maximum path sum.
In this question, a path is defined as a sequence starting from any node in the tree and reaching any node. The path contains at least one node and does not necessarily pass through the root node.
Example 1:
Input: [1,2,3]
1
/ \
2 3
Output: 6
Example 2:
Input: [-10,9,20,null,null,15,7]
-10
/ \
9 20
/ \
15 7
Output: 42
The following main function can be used:
#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;
}
Enter description:
First enter the number of nodes n (note that the nodes here include the null nodes in the question)
Then enter the data of n nodes. If you need to fill in the empty nodes, enter null.
Output description:
Output an integer representing the result.
input Output
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;
}