47二叉樹中的最大路徑和(124)
作者:
晚於: 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-/