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 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...

  2. c++英文单词频度统计程序

    英文单词频度统计程序(c++版) 写一个程序,分析一个文本文件(英文文章)中各个次出现的频率,并且把频率最高的十个词打印出来. 分析过程: (1)  简单设想大致分为两大步骤: 1.经过文本文件的读操 ...

  3. (BFS)poj2935-Basic Wall Maze

    题目地址 题目与最基本的BFS迷宫的区别就是有一些障碍,可以通过建立三维数组,标记某个地方有障碍不能走.另一个点是输出路径,对此建立结构体时要建立一个pre变量,指向前一个的下标.这样回溯(方法十分经 ...

  4. SoapUI中如何传递cookie

    import com.eviware.soapui.support.types.StringToStringMap //Get all the cookies in the response def ...

  5. Redisson-Parent 2.5.0 和 3.0.0 发布

    Redisson-Parent 2.5.0 和 3.0.0 发布了,Redisson 是基于 Redis 服务之上构建的分布式.可伸缩的 Java 数据结构,高级的 Redis 客户端. Rediss ...

  6. jQueryUI之交互

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. windows环境下XAMPP安装、多域名多端口配置、与python环境并存

    一.去xampp官网下载最新版本的安装包,安装一般软件的安装步骤,一直下一步,不过如果你想安装到指定目录中的话,在选择安装位置的时候设置想要安装的位置. 二.我们在工作中经常遇到同时调试多个网站的情况 ...

  8. 解决 Redis Cluster 扩容故障

    双11啦,为了给商品详细redis进行扩容,扩容动作就放在了今天晚上进行,很不巧,今天晚上是个多事之秋: 做了次数据恢复,做了次集群迁移,在迁移的时候还踩了个坑! 集群中有个节点挂掉了,并且报错信息如 ...

  9. An internal error occured during :"C/C++" . java.lang.NullPointerException

    用eclipse 导入cocos2d项目的时候报了这个错,导致项目在eclipse 里面是空的,反复导入也不行. 解决办法,把其他正常项目里面的proj.android目录下面的.cproject文件 ...

  10. C# Excel导入

    两张表导入到一个DataGrid里面(题目表和答案表) 前台代码 <asp:Content ID="Content1" ContentPlaceHolderID=" ...