Given a binary tree, find the maximum path sum.

For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root.

For example:
Given the below binary tree,

       1
/ \
2 3

Return 6.

/**
* 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 findMax(TreeNode *root,int &res)
{
if(root==NULL)return 0;
int value=root->val;
int left=findMax(root->left,res);
int right=findMax(root->right,res);
value+=left>0?left:0;
value+=right>0?right:0;
res=value>res?value:res;
return max(root->val,max(root->val+left,root->val+right));
}
int maxPathSum(TreeNode *root) {
int res=INT_MIN;
findMax(root,res);
return res;
}
};

  

二叉树最大路径和-Binary Tree Maximum Path Sum的更多相关文章

  1. [Swift]LeetCode124. 二叉树中的最大路径和 | 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 ...

  2. 二叉树中的最大路径和 · Binary Tree Maximum Path Sum

    [抄题]: 给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和) [思维问题]: 不会写分合法 [一句话思路]: 用两次分治:ro ...

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

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

  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 124. Binary Tree Maximum Path Sum (HARD)

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

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

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

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

随机推荐

  1. ANSI X9.8标准 PIN xor PAN获取PIN BlOCK

    ANSI X9.8标准 PIN xor PAN获取PIN BlOCK 之前看到几篇介绍,把ANSI说成16个字节,真心扯淡,各种误人子弟,真正的ANSI算法其实是8个字节,具体格式如下: (1) AN ...

  2. ajax编程**

    ajax 编程 *step1获得 XmlHttpRequest 对象.该对象由浏览器提供,但是该类型并没有标准化.ie 和其它浏览器不同,其它浏览器都支持该类型,而 ie 不支持. function ...

  3. 写一个程序,统计自己C语言共写了多少行代码,Github基本操作

    前言 在上一篇博客中,本人提到了自己的文件操作可以说是几乎没用过.现在想想,这也算是只在OJ上做题的一个弊端吧.虽然通过OJ做题是一个学习代码好手段,但其他方面也要多多涉猎才好,而不是说OJ用不到文件 ...

  4. Python异常处理体系

    1.Python内建异常体系结构 The class hierarchy for built-in exceptions is: BaseException  +-- SystemExit  +-- ...

  5. php 系列

    1.给 跑在windows 环境下的php, 安装redis 拓展.(installing Redis & Redis extension in PHP on XAMPP on windows ...

  6. 2016"百度之星" - 资格赛(Astar Round1) Problem E

    简单模拟题,耐心写就能过. #include <stdio.h> #include <math.h> #include<cstring> #include<c ...

  7. matlab字符串操作

    字符串转换函数 abs        字符串到ASCII转换dec2hex        十进制数到十六进制字符串转换fprintf        把格式化的文本写到文件中或显示屏上hex2dec   ...

  8. 鼠标滚轮事件 onmousewheel

    1.fiefox:DOMMouseScroll               detail      detail*(-40)=wheelDelta 除了firefox之外:mousewheel     ...

  9. Extjs4 up 和down的用法

    Extjs4.x中,每个组件都新增加了两个方法up()和down()方法.这两个方法都是用来获取组件的,下面我们来看下up()方法和down()方法的官方解释. Extjs4.x中,新增加了两个方法u ...

  10. POJ 1696 Space Ant

    极角排序 每次选择一个最外围的没选过的点,选择的时候需要利用极角排序进行选择 #include<cstdio> #include<cstring> #include<ve ...