作者:
晚于: 2020-08-05 12:00:00后提交分数乘系数50%
截止日期: 2020-08-12 12:00:00
问题描述 :
给定一个非空二叉树,返回其最大路径和。
本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。
示例 1:
输入: [1,2,3]
1
/
2 3
输出: 6
示例 2:
输入: [-10,9,20,null,null,15,7]
-10
/
9 20
/
15 7
输出: 42
可使用以下main函数:
#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;
}
int main()
{
TreeNode* root;
root=inputTree();
int res=Solution().maxPathSum(root);
cout<<res<<endl;
}
输入说明 :
首先输入结点的数目n(注意,这里的结点包括题中的null空结点)
然后输入n个结点的数据,需要填充为空的结点,输入null。
输出说明 :
输出一个整数,表示结果。
输入范例 :
输出范例 :
#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) {} }; class Solution { int res=-9999; public: int fun(TreeNode *root) { if(!root) return 0; // 递归计算左右子节点的最大贡献值 // 只有在最大贡献值大于 0 时,才会选取对应子节点 int left=max(fun(root->left),0);//选取至少大于0的节点 int right=max(fun(root->right),0) ; // 节点的最大路径和取决于该节点的值与该节点的左右子节点的最大贡献值 int pathsum=root->val+left+right; res=max(res,pathsum); // 返回节点的最大贡献值 return root->val+max(left,right) ; } int maxPathSum(TreeNode* root) { fun(root); return res; } }; 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; }
https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/solution/er-cha-shu-zhong-de-zui-da-lu-jing-he-by-leetcode-/