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.

找出任意两个节点之间的路径,并且该路径的值之和最大。

PS:关键在于递归函数的返回值,应该返回该节点的任意子节点到该节点父节点之间路径的最大值,即root->l返回的值应该为root->l的任意子节点到root能得到的最大值-root->val。同时在遍历时时刻检查sum与max的大小并更新max的值。

 /**
* 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) {
res=INT_MIN;
dfs(root);
return res;
} int dfs(TreeNode *root){
if(root==NULL){
return ;
}
int sum=root->val; int l=dfs(root->left);
int r=dfs(root->right);
if(l>) sum+=l;
if(r>) sum+=r;
res=max(res,sum);
int tmp=max(l,r);
return tmp>?tmp+root->val:root->val;
}
int res;
};

binary-tree-maximum-path-sum——二叉树任意一条路径上的最大值的更多相关文章

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

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

  3. LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)

    题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...

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

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

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

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

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

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

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

随机推荐

  1. linux 复制部分文件到另外的文件夹

    show the command: |xargs -i cp {} ../ 或者指定目录 |xargs -i cp {} /home/peter

  2. pytion3--class一个更实际的例子

    class一个更实际的例子 到目前为止,我们所看的大多数例子都是人为创造而且是独立完备的,其目的是为了帮助你把注意力集中在基础知识上.然而,本章的结尾是一个较大的例子,把我们所学的大多数概念都聚合在这 ...

  3. [python学习篇][廖雪峰][2][高级函数] map 和reduce

    我们先看map.map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 举例说明,比如我们有一个函数f(x)=x2,要把这个函数 ...

  4. BZOJ 4816 [Sdoi2017]数字表格 ——莫比乌斯反演

    大力反演出奇迹. 然后xjb维护. 毕竟T1 #include <map> #include <ctime> #include <cmath> #include & ...

  5. Microsoft发布新一代主机:Xbox One

    当 Xbox One 这个名字从 Microsoft 高管 Don Mattrick 口中被念出来时,现场直接沸腾了.按照 Mattrick 的说法这是一款 all-in-one 的主机,而其核心价值 ...

  6. for循环创建对象

    有时候奇怪的发现往list添加数据的时候,一直被最后一个元素覆盖,首先 ,我们得明白原理: 在new 一个对象的时候,对象的ID是唯一确定的:将对象add入list中时,放入list中的其实是对象的引 ...

  7. 标准C程序设计七---36

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  8. sql server2008 R2安装总结

    1,卸载注意 在卸载Microsoft SQL Server 2008 R2 安装程序(简体中文) 出现 :“警告 26003.无法卸载 Microsoft SQL Server 2008 R2 安装 ...

  9. ML | Naive Bayes

    what's xxx In machine learning, naive Bayes classifiers are a family of simple probabilistic classif ...

  10. 把Execl表格中的数据获取出来保存到数据库中

    比如我们遇到一些需要把execl表格中的数据保存到数据库中,一条一条保存效率底下而且容易出错,数据量少还好,一旦遇到数据量大的时候就会累死个人啊,下面我们就来把execl表格中数据保存到对应的数据库中 ...