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. python学习--Django mvc框架简介

    让我们一览 Django 全貌 urls.py 网址入口,关联到对应的views.py中的一个函数(或者generic类),访问网址就对应一个函数. views.py 处理用户发出的请求,从urls. ...

  2. (英文排版测试)Lorem Ipsum

    Lorem Ipsum Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt consequat pretiu ...

  3. 紫书第三章训练1 E - DNA Consensus String

    DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of ...

  4. 【转】hibernate延迟加载和抓取策略

    一.延迟加载 1.简单查询get,load 针对对象本身延迟或即时 当使用load方法来得到一个对象时,此时hibernate会使用延迟加载的机制来加载这个对象,即:当我们使用session.load ...

  5. iOS学习笔记47-Swift(七)泛型

    一.Swift泛型介绍 泛型是为Swift编程灵活性的一种语法,在函数.枚举.结构体.类中都得到充分的应用,它的引入可以起到占位符的作用,当类型暂时不确定的,只有等到调用函数时才能确定具体类型的时候可 ...

  6. Windows 上的 C++ 编译器

    Windows 上的 C++ 编译器主要有: MinGW (Minimalist GNU for Windows) TDM-GCC (TDM 是 Twilight Dragon Media 的缩写) ...

  7. java面试题之如何实现处理线程的返回值?

    有三种实现方式: 主线程等待法: 使用Thread类的join方法阻塞当前线程以等待子线程处理完毕: 通过Callable接口实现,通过FutureTask 或者线程池:

  8. django 报错 no such table: auth_user

    需要执行 python3 manage.py makemigrations python3 manage.py migrate 参考:http://arrayoverflow.com/question ...

  9. 我要好offer之 链表大总结

    单链表是一种递归结构,可以将单链表看作特殊的二叉树(我把它叫做一叉树) 单链表的定义: /** * Definition for singly-linked list. * struct ListNo ...

  10. js-控制浏览器和移动端的后退按钮 . popstate

    //控制浏览器和移动端的后退按钮 if (window.history && window.history.pushState) { $(window).on('popstate', ...