LeetCode(124) Binary Tree Maximum Path Sum
题目
Given a binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root.
For example:
Given the below binary tree,
1
/ \
2 3
Return 6.
分析
如果这个作为root,那么最长路应该就是..
F(left) + F(right) + val...当然如果left,或者right<0就不用加了的= =
从下往上递归遍历...
如果不这个不是root,那么就不能把left和right加起来了...因为只是一条路...
AC代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ class Solution {
public:
int maxVal = INT_MIN;
int maxPathSum(TreeNode* root) {
if (root == NULL)
return ; maxSum(root);
return maxVal;
} /*递归函数*/
int maxSum(TreeNode *root)
{
if (root == NULL)
return ; /*求以root为根的当前子树的最大路径和*/
int curVal = root->val;
int lmaxSum = maxSum(root->left), rmaxSum = maxSum(root->right);
if (lmaxSum > )
curVal += lmaxSum;
if (rmaxSum > )
curVal += rmaxSum; if (curVal > maxVal)
maxVal = curVal; /*返回以当前root为根的子树的最大路径和*/
return max(root->val, max(root->val + lmaxSum, root->val + rmaxSum));
}
};
LeetCode(124) Binary Tree Maximum Path Sum的更多相关文章
- (算法)Binary Tree Max Path Sum
题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seque ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 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
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- [leetcode]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- 【leetcode】Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- LeetCode: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
随机推荐
- JQuery实现的模块交换动画效果
<!doctype html> <html> <head> <meta http-equiv="content-type" content ...
- 考查SQLite 3索引对整数排序的性能影响
做个实验,想了解SQLite3索引对整数排序的性能影响. 用这个测试表,考查绿色那列: id name date 自增型主键 字符串型,随机生成 整数型 随机生成,范围0到54354354 1 bMz ...
- bash coding to changeNames
____通配符和正则表达式 此处的定义只针对linux 中的shell语言,对其它语言不适用 _正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配.grep.awk.sed等命令可以支持正则表 ...
- CSS解决高度自适应问题
HTML结构如下: <div id="main"> <div id="top">top</div> < ...
- Python 无限循环
import threading import time class CountDownTimer(threading.Thread): def __init__(self, seconds, act ...
- 安装oracle ebs 出现问题 atleast 55M of disk space
出现这个问题 可以进行如下尝试: 1.登陆用户是否具有管理员权限,可以右击exe用管理员登陆. 2.安装电脑是否有安装mcafee,可以禁用后运行. 3.看下C:\Documents and Sett ...
- PLSQL win7 64位
1. 解压instantclient-basic-win32-11.2.0.1.0.zip至Oracle安装目录的Product下 具体目录如下D:\Oracle\product\instantcli ...
- ArcMap 操作笔记
1.SQL查询(in) select * from table where PointID in ('1','2')
- python 识别图片验证码报IOError
说一下困扰了我一周的问题:识别图片验证码 本来我按照安装步骤(http://www.cnblogs.com/yeayee/p/4955506.html?utm_source=tuicool&u ...
- [html]LESS-1.3.3
网站:http://www.bootcss.com/p/lesscss/ 下载链接:http://files.cnblogs.com/files/z5337/less-1.3.3.min.js