题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和。(点权有负的。)

思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值。要注意如果子树传来的如果是负值,是可以同时丢弃的,但至少要将当前节点的val更新答案。

 /**
* 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 maxPathSum(TreeNode* root) {
ans=-;
DFS(root);
return ans;
}
private:
int ans;
int DFS(TreeNode* t) {
if(!t) return ;
int a=max(,DFS(t->left)); //小于0的直接丢弃
int b=max(,DFS(t->right));
ans=max(ans, a+b+t->val); //更新,必须以当前节点为中转
return max(a, b)+t->val; //只能返回一条到叶子的路径
}
};

AC代码

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

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

  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]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: Binary Tree Maximum Path Sum 解题报告

    Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...

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

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

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

  9. [leetcode]Binary Tree Maximum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...

随机推荐

  1. POJ 2010

    Moo University - Financial Aid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4235   A ...

  2. MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-3]

    引入Struts2 在pom.xml中加入jar包 <!-- struts2 --> <dependency> <groupId>org.apache.struts ...

  3. 集成 Tomcat 插件到 Eclipse 的过程

    Java代码: . 下载 Tomcat Tomcat6,下载地址:http://tomcat.apache.org/download-60.cgi,选择绿色版的 zip 进行下载(目前最新的 Tomc ...

  4. lintcode:数字组合I

    数字组合I 给出一组候选数字(C)和目标数字(T),找到C中所有的组合,使找出的数字和为T.C中的数字可以无限制重复被选取. 例如,给出候选数组[2,3,6,7]和目标数字7,所求的解为: [7], ...

  5. hdu 2897 邂逅明下

    转: 这个游戏和Bash game差不多,只不过是Bash game说的是每次最少取一个,最多m个,这个游戏限制在p 和q之间而已,若最后不足p个,那么就一次取完.而且该游戏要求的是最后取光的人输. ...

  6. Android核心分析之十八Android电话系统之RIL-Java

    Android RIL-Java 123.jpg (2.09 KB, 下载次数: 1) 下载附件  保存到相册 2012-3-21 10:47 上传   RIL-Java在本质上就是一个RIL代理,起 ...

  7. 基于RPC原理的dubbo

    在校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 而一旦踏入公司尤其是大型互联网公司就会发现,公司的系 ...

  8. 【转】SIP初步

    1.什么是SIP SIP(会话发起协议)属于IP应用层协议,用于在IP网上为用户提供会话应用.会话(Session)指两方或多方用户之间的语音.视频.及其他媒体形式的通信,具体可能是IP电话.会议.即 ...

  9. 296. Best Meeting Point

    题目: A group of two or more people wants to meet and minimize the total travel distance. You are give ...

  10. swift:Optional Type 、Swift和Objective-C混编的讲解

    ❤️❤️❤️swift中的Optional Type的?和!含义:其实就是一个装包和拆包的过程 optional的含义: Optional事实上是一个枚举类型,Optional包含None和Some两 ...