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.

算法:(有点动态规划的思想)首先明确要自底向上进行计算,只考虑当前节点root,我们从子节点向上返回的是一条线路目前的最大值,该线路最高层(最上面)的节点是该子节点,并且该子节点不能同时有左右分支(即在线路上不能同时有左右分支,如果有分支,root返回到上一节点后,不能形成一个链,会在root出现分叉),然后我们比较root->val,

root->val+leftMax(从左子树返回的值),root->val+rightMax(从右子树返回的值),其中最大的就是root节点应该向上返回的值,我们在计算root节点的返回值时,可以顺便计算以root为最高层节点的链的线路最大值(可以有分叉),只要比较,root->val,  leftMax+root->val,  root->val+rightMax,   leftMax+rightMax+root->val,其中最大的就是以root为最高层次节点的线路的最大值,然后用这个最大值,和最初保存的最大值result(初始化为INT_MIN)做比较,如果大于result,更新result,最终result就是结果,代码如下,时间复杂度O(n),只需要遍历一边二叉树(后序遍历):

 /**
* 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 result;
int maxPathSum(TreeNode *root) {
result=INT_MIN;
getMax(root);
return result;
}
int getMax(TreeNode* root)
{
if(root==NULL) return ;
int leftMax=getMax(root->left);
int rightMax=getMax(root->right);
int tmax=max(max(leftMax+root->val,max(rightMax+root->val,root->val)),leftMax+rightMax+root->val);
if(tmax>result) result=tmax;
int rootmax=max(root->val,max(root->val+leftMax,root->val+rightMax));
return rootmax;
}
};

Binary Tree Maximum Path Sum的更多相关文章

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

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

  3. 26. Binary Tree Maximum Path Sum

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

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

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

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

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

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

  8. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

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

  10. 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 sequen ...

随机推荐

  1. zookeeper命令行(zkCli.sh&zkServer.sh)使用及四字命令

    zookeeper提供了很多方便的功能,方便我们查看服务器的状态,增加,修改,删除数据(入口是zkServer.sh和zkCli.sh). 还提供了一系列四字命令,方便我们跟服务器进行各种交互,来确认 ...

  2. windows dir改成ls

    习惯了linux下的ls命令,windows的dir用的很不习惯,又不想装cygwin, bash,就想把dir重命名为ls,发现dos下有个命令doskey可以完成该功能.在命令提示符下敲: > ...

  3. Opencv读取与显示图片

    #include "stdafx.h"#include "cv.h"#include "cxcore.h"#include "hi ...

  4. iframe的使用

    function Report() {           var info = document.getElementById("iframeReport");          ...

  5. webpack 代码拆分,按需加载

    转自:https://segmentfault.com/a/1190000007649417?utm_source=weekly&utm_medium=email&utm_campai ...

  6. 三个loading小动画实例

    直接贴代码: <!DOCTYPE html><html><head>    <meta charset="utf-8">    &l ...

  7. ASP.NET 下拉列表绑定枚举类型值,不用再新建一个枚举表

    public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArg ...

  8. coderforces719b

    题目大意:给定一个字符串,这个字符串中只有“r”和"b"组成,每次操作只能交换两个字符的位置或者将一个字符由"r"变"b"(或由" ...

  9. [转]App Store 审核、限时免费、排行、推荐机制技巧精华汇总

    在 App Store 上,什么样的应用会得到推荐? 这个问题问的非常大,而且编辑推荐很多个人元素在里面,我试着用推荐Ovi Store应用的思路来回答一下: 关于应用: 1.首先这个应用最基本的功能 ...

  10. 为什么要用Maven?

    早期还在学怎么用Ant构建项目时,就有看到说Maven是Ant的替代品,但真正意义去了解Maven,还是因为以前的公司一老员工在做OpenJMS二次开发时,从网上下载了源码,然后用Maven构建它. ...