NC6. 二叉树中的最大路径和
描述
示例1
输入:
{1,2,3}
输出:
6
示例2
输入:
{-20,8,20,#,#,15,6}
输出:
41
说明:
其中一条最大路径为:15=>20=>6,路径和为15+20+6=41示例3
输入:
{-2,#,-3}
输出:
-2
C++ 解法, 执行用时: 28ms, 内存消耗: 8024KB, 提交时间: 2022-05-08
static int x=[]{ std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }(); class Solution { public: int ans = -0x3f3f3f3f; int dfs(TreeNode* root) { if (!root) return 0; int l = dfs(root->left); int r = dfs(root->right); int m = root->val; ans = max(ans, m + (r > 0 ? r : 0) + (l > 0 ? l : 0)); return m + max(max(l, r), 0); } int maxPathSum(TreeNode* root) { return max(ans, dfs(root)); } };
C++ 解法, 执行用时: 31ms, 内存消耗: 8592KB, 提交时间: 2022-04-12
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ static int x=[]{ std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }(); class Solution { public: /** * * @param root TreeNode类 * @return int整型 */ int ans = -0x3f3f3f3f; int dfs(TreeNode* root) { if (!root) return 0; int l = dfs(root->left); int r = dfs(root->right); int m = root->val; ans = max(ans, m + (r > 0 ? r : 0) + (l > 0 ? l : 0)); return m + max(max(l, r), 0); } int maxPathSum(TreeNode* root) { // write code here return max(ans, dfs(root)); } };
C++ 解法, 执行用时: 31ms, 内存消耗: 9472KB, 提交时间: 2022-04-06
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ static int x=[]{ std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }(); class Solution { public: /** * * @param root TreeNode类 * @return int整型 */ int maxl; int maxPathSum(TreeNode* root) { if(root==NULL) return 0; maxl=root->val; maxl=max(dfs(root),maxl); return maxl; // write code here } int dfs(TreeNode* root){ int tm; if(root->left==NULL&&root->right==NULL){ maxl = max(root->val,maxl); return root->val; } else if(root->left==NULL){ tm = dfs(root->right); maxl = max(maxl,max(root->val,max(tm,tm+root->val))); } else if(root->right==NULL){ tm = dfs(root->left); maxl = max(maxl,max(root->val,max(tm,tm+root->val))); } else{ int lm = dfs(root->left); int rm = dfs(root->right); tm = max(rm,lm); maxl = max(tm,max(lm+rm+root->val,maxl)); } tm =max(tm+root->val,root->val); return tm; } };
C++ 解法, 执行用时: 32ms, 内存消耗: 8612KB, 提交时间: 2022-05-27
static const auto io_sync_off = [](){ std::ios::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); return nullptr; }(); class Solution { public: int res=INT_MIN; int dfs(TreeNode* root) { if(!root) return 0; int l=dfs(root->left); int r=dfs(root->right); int m=root->val; if(l>0) m+=l; if(r>0) m+=r; res=max(m,res); return max(root->val,max(l,r)+root->val); } int maxPathSum(TreeNode* root) { dfs(root); return res; } };
C++ 解法, 执行用时: 32ms, 内存消耗: 9520KB, 提交时间: 2022-04-29
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @return int整型 */ int Maxsum = -1000; int maxPathSum(TreeNode* root) { // write code here dfs(root); return Maxsum; } int dfs(TreeNode* node) { if(node == nullptr) return 0; int left, right; left = max(dfs(node->left), 0); right = max(dfs(node->right), 0); Maxsum = max(Maxsum, node->val + left + right); return max(node->val + left, node->val + right); } };