[leetcode]Binary Tree Maximum Path Sum
来源:互联网 发布:域名备案系统 编辑:程序博客网 时间:2024/06/14 10:21
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void dfs(TreeNode *root, int &sum, int &result) {
if (root == NULL) {
sum = 0;
return;
}
int s1, s2;
dfs(root->left, s1, result);
dfs(root->right, s2, result);
sum = max(root->val, max(root->val + s1, root->val + s2));
int tot = root->val;
if (s1 > 0) tot += s1;
if (s2 > 0) tot += s2;
if (tot > result) result = tot;
}
int maxPathSum(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int result = INT_MIN;
int sum;
dfs(root, sum, result);
return result;
}
};
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void dfs(TreeNode *root, int &sum, int &result) {
if (root == NULL) {
sum = 0;
return;
}
int s1, s2;
dfs(root->left, s1, result);
dfs(root->right, s2, result);
sum = max(root->val, max(root->val + s1, root->val + s2));
int tot = root->val;
if (s1 > 0) tot += s1;
if (s2 > 0) tot += s2;
if (tot > result) result = tot;
}
int maxPathSum(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int result = INT_MIN;
int sum;
dfs(root, sum, result);
return result;
}
};
- *****(leetcode) Binary Tree Maximum Path Sum (tree)
- LeetCode Binary Tree Maximum Path Sum
- [Leetcode] Binary Tree Maximum Path Sum
- [leetcode] Binary Tree Maximum Path Sum
- LeetCode Binary Tree Maximum Path Sum
- [LeetCode] Binary Tree Maximum Path Sum
- leetcode Binary Tree Maximum Path Sum
- 【leetcode】Binary Tree Maximum Path Sum
- LeetCode - Binary Tree Maximum Path Sum
- [LeetCode]Binary Tree Maximum Path Sum
- [Leetcode]Binary Tree Maximum Path Sum
- Leetcode: Binary Tree Maximum Path Sum
- LeetCode-Binary Tree Maximum Path Sum
- [leetcode] Binary Tree Maximum Path Sum
- LeetCode - Binary Tree Maximum Path Sum
- LeetCode 124: Binary Tree Maximum Path Sum
- 【Leetcode】Binary Tree Maximum Path Sum
- LeetCode | Binary Tree Maximum Path Sum
- hdu 1175连连看
- 黑马程序员——异常
- 转的C++库资料
- 【源码】java中图片和Base64互相转换源码
- Java类加载原理解析
- [leetcode]Binary Tree Maximum Path Sum
- Xcode学习笔记---ViewController的相关说明
- mysql基础操作
- android 命令行输出日志
- 使用PXE+DHCP+Apache+Kickstart无人值守安装CentOS5.8 x86_64
- eclipse修改字体
- differences between dp , sp, px, pt
- tomcat下配置https环境
- Dijkstra 证明