Binary Tree Maximum Path Sum
来源:互联网 发布:阿里云备案是什么意思 编辑:程序博客网 时间:2024/06/07 15:51
-----QUESTION-----
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
Return
6
.-----SOLUTION-----
class Solution {public: int maxPathSum(TreeNode *root) { maxSum = INT_MIN; calRootSum(root); return maxSum; } int calRootSum(TreeNode *root) { int leftSum = 0; int rightSum = 0; if(root->left) { leftSum = calRootSum(root->left); } if(root->right) { rightSum = calRootSum(root->right); } int sum = root->val; if(leftSum > 0) { sum += leftSum; } else { leftSum = 0; } if(rightSum > 0) { sum += rightSum; } else { rightSum = 0; } if(sum > maxSum) { maxSum = sum; } sum = max(leftSum,rightSum); return sum+root->val; }private: int maxSum;};
0 0
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- 郑州好的治疗抑郁症的医院
- 算法2,用递归的模式实现二分查找
- HDU 2016 数据的交换输出
- 算法->Trie树 兄弟单词
- uva10706 - Number Sequence(找规律)
- Binary Tree Maximum Path Sum
- HDU 2017 字符串统计
- JAVA核心技术的学习
- 导出pdf
- i++,++i,i+=1,i=i+1在gcc下汇编
- Flow-tools and FlowView to monitor the netflow in Openstack
- ResGen.exe”已退出,代码为2 问题处理
- POJ 2409 Let it Bead
- 一步一步优化你的mysql------创建高性能的索引