【LeetCode】124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum
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,
1
/ \
2 3
Return 6.
树结构显然用递归来解,解题关键:
1、对于每一层递归,只有包含此层树根节点的值才可以返回到上层。否则路径将不连续。
2、返回的值最多为根节点加上左右子树中的一个返回值,而不能加上两个返回值。否则路径将分叉。
在这两个前提下有个需要注意的问题,最上层返回的值并不一定是满足要求的最大值,
因为最大值对应的路径不一定包含root的值,可能存在于某个子树上。
因此解决方案为设置全局变量maxSum,在递归过程中不断更新最大值。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxSum;
Solution()
{
maxSum = INT_MIN;
}
int maxPathSum(TreeNode* root)
{
Helper(root);
return maxSum;
}
int Helper(TreeNode *root) {
if(!root)
return INT_MIN;
else
{
int left = Helper(root->left);
int right = Helper(root->right);
if(root->val >= )
{//allways include root
if(left >= && right >= )
maxSum = max(maxSum, root->val+left+right);
else if(left >= && right < )
maxSum = max(maxSum, root->val+left);
else if(left < && right >= )
maxSum = max(maxSum, root->val+right);
else
maxSum = max(maxSum, root->val);
}
else
{
if(left >= && right >= )
maxSum = max(maxSum, max(root->val+left+right, max(left, right)));
else if(left >= && right < )
maxSum = max(maxSum, left);
else if(left < && right >= )
maxSum = max(maxSum, right);
else
maxSum = max(maxSum, max(root->val, max(left, right)));
}
//return only one path, do not add left and right at the same time
return max(root->val+max(, left), root->val+max(, right));
}
}
};

【LeetCode】124. Binary Tree Maximum Path Sum的更多相关文章
- 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- Leetcode solution 124: Binary Tree Maximum Path Sum
Problem Statement Given a non-empty binary tree, find the maximum path sum. For this problem, a path ...
- 【Lintcode】094.Binary Tree Maximum Path Sum
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- leetcode@ [124] Binary Tree Maximum Path Sum (DFS)
https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...
- [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)
题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as ...
随机推荐
- python垃圾回收杂谈
当创建对象时Python立即向操作系统请求内存.每当对象的引用数减为0,Python垃圾回收器立刻挺身而出,立即将其释放,把内存还给操作系统.在Python中,每个对象都保存了一个称为引用计数的整数值 ...
- 数据库实例: STOREBOOK > 表空间 > 编辑 表空间: SYSAUX
ylbtech-Oracle:数据库实例: STOREBOOK > 表空间 > 编辑 表空间: SYSAUX 表空间 > 编辑 表空间: SYSAUX 1. 一般信息返 ...
- 【Todo】React & Nodejs学习 &事件驱动,非阻塞IO & JS知识栈:Node为主,JQuery为辅,Bootstrap & React为辅辅,其他如Angular了解用途即可
JS知识栈:Node为主,JQuery为辅,Bootstrap & React为辅辅,其他如Angular了解用途即可 今天在学习ReactJS和NodeJS,看到关于ReactJS的这篇文章 ...
- 安装Oracle之后解决掉的问题分享
TNS-03505: 无法解析名称 在测试tnsping的时候始终显示这么个问题. ...
- maven编译报错 -source 1.7 中不支持 lambda 表达式
Maven项目编译失败: [ERROR] COMPILATION ERROR : [INFO] ---------------------------------------------------- ...
- 【7】AccessDB快速数据访问
阅读目录 C#和VB数据访问的比较 AccessDB的设计 数据库的连接 三种主要操作 错误输出及调试 小结 回到顶部 C#和VB数据访问的比较 C#中要进行一次普通的数据库查询,需要创建连接,再根据 ...
- [Node.js] Child Process with fork() to handle heavy calculation process
When build server, if we have a API endpoint requires some heavy calculation process, it will block ...
- JavaScript实现把数字转换成中文
/** * 数字转换汉字大写 * @constructor * 用法示例:new NumberToChinese(122222).toUpper(); new NumberToChinese(1222 ...
- struts2基础梳理(二)
本篇主要有:设置struts2匹配的扩展名.使用通配符,值栈,声明式异常以及标签. 设置扩展名: 默认是对.action和不加不论什么扩展名的进行处理.能够设置: <constant name= ...
- Discuz常见大问题-如何开启和使用首页四格
在论坛-首页四格中,勾选开启首页四格,然后可以选择数据来源的板块 注意首页四格刷新是有时间的,5分钟左右,不是你这里更新完了帖子那里就有了(如果你自己建的网站,可能回复和热帖都还没有) 当然你也可以使 ...