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 a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int getMax(TreeNode* root, int &ans)
{
if(!root)
return ;
int leftMax = getMax(root->left, ans), rightMax = getMax(root->right, ans), Max = max(leftMax, rightMax);
int curMax = ((leftMax > ) ? leftMax : ) + ((rightMax > ) ? rightMax : ) + root->val;
if(curMax > ans)
ans = curMax;
if(Max <= )
return root->val;
return Max + root->val;
}
int maxPathSum(TreeNode* root) {
int ans = INT_MIN;
getMax(root, ans);
return ans;
}
};

分别求左右子树的最大节点和,若为负,则忽略,若为正,则加上本身的节点值。

124. Binary Tree Maximum Path Sum *HARD* -- 二叉树中节点和最大的路径的节点和的更多相关文章

  1. [LeetCode] 124. 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. 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 ...

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

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

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

  5. [leetcode]124. 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 ...

  6. LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)

    题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as ...

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

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

  9. LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

随机推荐

  1. FPN(feature pyramid networks)

    多尺度的object detection算法:FPN(feature pyramid networks). 原来多数的object detection算法都是只采用顶层特征做预测,但我们知道低层的特征 ...

  2. 小米范工具系列之一:小米范 web查找器

    最新版本1.5,下载地址:http://pan.baidu.com/s/1c1NDSVe  文件名web finder,请使用java1.8运行 小米范 web查找器主要功能为快速端口扫描,并识别we ...

  3. java-mybaits-009-mybatis-spring-使用,SqlSessionFactoryBean、事务

    一.版本限制 参看地址:http://www.mybatis.org/spring/ 二.使用入门 2.1.pom <dependency> <groupId>org.myba ...

  4. 详解PHP实现定时任务的五种方法

    这几天需要用PHP写一个定时抓取网页的服务器应用. 在网上搜了一下解决办法, 找到几种解决办法,现总结如下. 定时运行任务对于一个网站来说,是一个比较重要的任务,比如定时发布文档,定时清理垃圾信息等, ...

  5. uva The Tower of Babylon[LIS][dp]

    转自:https://mp.weixin.qq.com/s/oZVj8lxJH6ZqL4sGCXuxMw The Tower of Babylon(巴比伦塔) Perhaps you have hea ...

  6. Hadoop 编写WordCount

    本文发表于本人博客. 前面几次讲了关于Hadoop的环境搭建.HDFS操作,今天接着继续.本来Hadoop源码中就有一个例子WordCount,但是今天我们来自己实现一个加深对这个Mapper.Red ...

  7. mysql索引之哈希索引

    哈希算法 哈希算法时间复杂度为O(1),且不只存在于索引中,每个数据库应用中都存在该数据结构. 哈希表 哈希表也为散列表,又直接寻址改进而来.在哈希的方式下,一个元素k处于h(k)中,即利用哈希函数h ...

  8. ssm所需要的pom(jre8、tomcat8、spring4)

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  9. 2016-2017 CT S03E07: Codeforces Trainings Season 3 Episode 7

    B. Pen Pineapple Apple Pen Solved. 题意:将一个序列合并成一个数. 思路:分类讨论一下, 水. #include<bits/stdc++.h> using ...

  10. uva1391 2-SAT 问题

    题意在大白书上. 有3 种工作 abc 大于等于平均年龄的可以去做a c 工作, 小于平均年龄的可以去做 bc , 同样转化为2 -sat 去做, 因为对于每个人也只有2 种情况可以作为选择 #inc ...