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.

思想: 后序遍历。注意路径的连通: 结点不为空时要返回  max( max(leftV, rightV)+rootV, rootV);

/**
* 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) {
getMaxSum(root);
return _maxPathSum;
}
protected:
int getMaxSum(TreeNode *root) {
if(root == NULL) return Min_Val;
int left = getMaxSum(root->left);
int right = getMaxSum(root->right);
return findMax(left, right, root->val);
}
int findMax(int left, int right, int rootV) {
int PathSum = max(max(left, right)+rootV, rootV);
_maxPathSum = max(_maxPathSum, max(PathSum, rootV + left + right));
return PathSum;
}
private:
enum{ Min_Val = -1000};
int _maxPathSum = Min_Val;
};

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

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

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

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

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

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

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

  8. 【二叉树的递归】05二叉树中找任意起点和终点使他们的路径和最大【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. ...

随机推荐

  1. 笔记本设置wifi热点

    http://jingyan.baidu.com/article/335530da4f774019cb41c3eb.html 注意: 1. 适配器设置中是设置的“本地连接”的属性,不要搞错了. 2. ...

  2. js异步加载

    默认情况javascript是同步加载的,也就是javascript的加载时阻塞的,后面的元素要等待javascript加载完毕后才能进行再加载,对于一些意义不是很大的javascript,如果放在页 ...

  3. 安装了VS2010 sp1 后再安装ASP.NET MVC 3.0的问题

    安装了VS2010 sp1 后再安装ASP.NET MVC 3.0的问题(Final Result: Installation failed with error code: (0x80070643) ...

  4. c# windows编程控件学习-1

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 探索javascript----有关数组的常用方法

    与字符串的转换: str.split(","):将一个字符串以为英文逗号分割,返回一个数组: arr.join(","):返回用指定分隔符间隔的含所有数组元素的 ...

  6. zipalign内存对齐优化

    zipalign:android中SDK下tools文件夹 用来对资源文件的内存进行对齐优化 手工命令: 优化:zipalign -v 4 source.apk destination.apk 4: ...

  7. 模仿MFC封装Windows API

    .... 最后添加了两个按钮,分别处理每个按钮的单击事件时,走了弯路,本来想的是在QButton中重写OnLButtonDown方法,但是,无法区分是那个按钮.参考这篇文章: http://zhida ...

  8. 超级链接a中javascript:void(0)弹出另外一个框问题

    转字:http://my.oschina.net/castusz/blog/68186 结果在IE.Firefox.Chrome都是先执行的onclick事件,在项目中我们尽量不要同时使用这两种方式. ...

  9. Umap2:开源USB host安全评估工具

    Umap2是一款由NCC Group和Cisco SAS小组开发的.基于python的USB host安全评估工具. 它拥有第一版所支持的所有功能: umap2emulate:USB设备枚举 umap ...

  10. mouseover 移入某个元素后停留一段时间再执行函授,我用于解决轮播图下面计数用的元素快速移入后会出BUG的问题。

    var stop; $(this).bind("mouseover",function(){ stop= setTimeout(function(){ },200); }).bin ...