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

Return6.

思路:题目中说明起始节点可以是任意节点,所以,最大的路径和不一样要经过root,可以是左子树中某一条,或者是右子树中某一条,当然也可能是经过树的根节点root的。递归式是应该是这三者中选出最大者。这题是看完yucoding的博客才算可能理解,这里只是用中文讲解该博客中的分析过程。举例子:

对树中的任一节点,当有一条路径经过它时(不一定为最大),有两种情况:

1)“顶节点”为当前节点时,如当前节点为2时,路径为6->4->2->5->-3;

2)“顶节点”为当前节点的父节点1,当前节点为2时,路径为-3->5->2->1->-3->6

对某个节点a,最大路径为:

i) max_top(a)为第一种情况下的最大路径和;

ii) max_single(a)为第二种情况下的最大路径和;

则,max_top(a)=Max{max_single(a), max_single(a->left)+max_single(a->right)+a->val, a->val};

max_single(a)=Max{max_single(a->left)+a->val, max_single(a->right)+a->val, a->val};

最每个节点a,res=max(res, max_top(a))。

其实,个人这样理解的,以当前点为“顶结点”,则,需从只有一条子树的和、两条子树加顶点的和、该顶点的值三种中选出最大值作为所求值;若以当前点的父结点为顶结点,说明这条路径必须经过该父结点,所以,求经过当前结点的路径,只能是从叶结点到当前结点(再到父结点),即只有一条而不能是两条之和,若是再求两条之后,则后续就不能通过该父结点了。

 /**
* 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 maxPathSum(TreeNode *root)
{
int res = root->val;
maxPathSumDFS(root, res);
return res;
}
int maxPathSumDFS(TreeNode *root, int &res) {
if (!root) return ;
int left = maxPathSumDFS(root->left, res);
int right = maxPathSumDFS(root->right, res);
int top = root->val + (left > ? left : ) + (right > ? right : ); //第一种
res = max(res, top);
return max(left, right) > ? max(left, right) + root->val : root->val; //第二种
}
};

//代码来源Grandyang

[Leetcode] Binary tree maximum path sum求二叉树最大路径和的更多相关文章

  1. [LeetCode] 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. ...

  2. [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 ...

  3. LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

  4. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

  5. [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 ...

  6. 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 ...

  7. [LeetCode] 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. ...

  8. [leetcode]Binary Tree Maximum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...

  9. leetcode–Binary Tree Maximum Path Sum

    1.题目说明 Given a binary tree, find the maximum path sum.   The path may start and end at any node in t ...

随机推荐

  1. spring-运行时值注入

    在项目中经常使用连接数据库的配置,如下所示 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDa ...

  2. ECSHOP和SHOPEX快递单号查询百世快递插件V8.6专版

    发布ECSHOP说明: ECSHOP快递物流单号查询插件特色 本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅 ...

  3. hack游戏攻略(梦之光芒黑客小游戏)

    2019.2.11 继续玩~~还是黑客游戏闯关类的 地址:http://monyer.com/game/game1/ 直接查看页面代码: first.php就是了: 查看源代码: 这里尝试输入 两个空 ...

  4. go学习笔记-常见命令

    常见命令 go 命令 可以在控制台执行go来查看 go Go is a tool for managing Go source code. Usage: go <command> [arg ...

  5. Hue 工具使用

    Hue 是一个 Web 接口的 Hadoop 分析数据工具,由 Cloudra 公司开源 官方网址 Github 地址 -> 安装方法 文档地址 一.Build 1.ubuntu安装所需环境(以 ...

  6. linux安装软件的几种方式(kali平台)和一些实用的软件(持续更新)

    安装软件前我们先更改镜像源,编辑 /etc/apt/sources.list 文件, 在文件最前面添加以下条目: #中科大更新源 deb https://mirrors.ustc.edu.cn/kal ...

  7. 2426: [HAOI2010]工厂选址

    2426: [HAOI2010]工厂选址 链接 代码: /* 贪心: 奇妙!!!!! 因为所有的煤矿不是给新厂,就是给旧厂(而且旧厂的得到b) 为了使费用最小,感性的理解,那么一个煤矿给哪个厂,取决于 ...

  8. malloc函数分配失败处理的严重性

    本次在实际测试情况下,发现程序无缘无故的异常,导致看门狗超时复位,经过排查是malloc函数分配失败的时候,依然对指针进行了操作,导致异常.以前没重视这个问题是因为,总觉的malloc基本都会成功的, ...

  9. Android TV 开发(3)

    本文来自网易云社区 作者:孙有军   <LinearLayout         android:id="@+id/input_num_line_3"         and ...

  10. Python request 简单使用

    Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...