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. POJ 3694 tarjan 桥+lca

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7298   Accepted: 2651 Descripti ...

  2. Java 导出EXCEL

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  3. C#日志记录函数

    错误日志记录在程序运行的实际维护中定位问题具有很大作用,日志越详细,反馈处理问题越方便. 常用的一个B/S架构下的日志函数. //日志记录函数 private void WriteLog( strin ...

  4. SQL同列合并

    SELECT cast(id as varchar(8)) new_id FROM tourol_Atractions where tcid>0 order by new_id SELECT t ...

  5. 国产AR SDK介绍

    说到VR,大家都知道虚拟现实有多火.可是VR之后呢,还有AR.相较于VR,AR的应用意义更加的强大. 相信在不久的将来AR和VR将会融为一体,把现实世界的数据信息完全联通在我们的眼前.这其中的领头羊莫 ...

  6. C#中用schema验证xml的合法性

    class ValidateXML { public string ErrString = string.Empty; public void ValidationEventCallBack(Obje ...

  7. 修改LR自带的示例程序端口号

    问题:LoadRunner的HP Web Tours 应用程序服务启动不了,提示1080端口被占用的问题 解决方法: 查看占用1080端口的进程 Cmd 窗口输入netstat –ano  找到占用该 ...

  8. fastqc, Per Base Sequence Content

    Per Base Sequence Content对所有reads的每一个位置,统计ATCG四种碱基(正常情况)的分布: 横轴为位置,纵轴为百分比. 正常情况下四种碱基的出现频率应该是接近的,而且没有 ...

  9. 今天<人人都能弹吉他>免费版获得了苹果的新品推荐

    今天改了一天程序, 来回测来回改, 准备提交一个新版本了. 傍晚跑步回来, 看了一下今天的下载量, 竟然比昨天多了. 然后就想, 难不成被苹果推荐了? 上线一看, 果然, 而且美国和中国两大市场都在新 ...

  10. php部分---创建连接数据库类

    class DBDA { public $host="localhost"; public $uid="root"; public $pwd="123 ...