【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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 sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
Example 1:
Input: [1,2,3]
       1
      / \
     2   3
Output: 6
Example 2:
Input: [-10,9,20,null,null,15,7]
   -10
   / \
  9  20
    /  \
   15   7
Output: 42
题目大意
找出二叉树中的最大路径和。
解题方法
递归
路径是我们可以从二叉树中任意选择1个或者多个相邻的节点而构成的。那么对于每个节点,我们是不是可以考虑他的左孩子是否考虑到路径内,右孩子是否考虑到路径内,从而产生公式:
经过一个节点的最大路径 = max(其左孩子为顶点的最大路径, 0) + max(右孩子为顶点的最大路径, 0) + 该节点的值。
公式里对左右孩子为顶点的最大路径和0取max,是因为路径可能是负值,加入左右孩子的最大路径为负数,那么就不应该使用了。
为什么左右孩子要为顶点的时候才行呢?一条路径不应该有分叉的,所以如果想求经过一个节点的路径的话,那么左右孩子那里不能分叉,必须是以左右孩子为出发点的一条路径:
   2
   / \
  9  20
    /  \
   15   7
最大路径是9 + 2 + 20 + 15
C++代码如下:
/**
 * 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) {
        if (!root) return 0;
        maxPathToLeaf(root);
        return res;
    }
    int maxPathToLeaf(TreeNode* root) {
        if (!root) return 0;
        int left = maxPathToLeaf(root->left);
        int right = maxPathToLeaf(root->right);
        if (left < 0)
            left = 0;
        if (right < 0)
            right = 0;
        res = max(res, left + right + root->val);
        return root->val + max(left, right);
    }
private:
    int res = INT_MIN;
};
相似题目
日期
2019 年 9 月 27 日 —— 昨天面快手,竟然是纯刷题
【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)的更多相关文章
- 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 ... 
- 第四周  Leetcode 124. Binary Tree Maximum Path Sum (HARD)
		124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ... 
- 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 ... 
- [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 ... 
- leetcode@ [124] Binary Tree Maximum Path Sum (DFS)
		https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ... 
- [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 ... 
- Java for LeetCode 124 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. ... 
- 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 ... 
- 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 ... 
随机推荐
- 【R】如何将重复行转化为多列(一对一转化一对多)?
			目录 需求 方法一 方法二 需求 一个数据框一列或多列中有重复行,如何将它的重复行转化为多列?即本来两列一对一的关系,如何转化为一对多的关系?普通的spread函数实现较为麻烦. 示例数据如下: It ... 
- python14异常处理
			def test_div(num1,num2): return num1 / num2 if __name__ == "__main__": try: print(test_div ... 
- Python基础之基本运算符
			目录 1. 算数运算符 2. 比较运算符 3. 赋值运算符 4. 逻辑运算符 5. 身份运算 6. 运算符优先级 1. 算数运算符 常用算术运算符使用方法如下: x = 5 y = 2 a = x + ... 
- Mssql主备见证的弊端及主备模式主down掉怎么恢复
			mssql主备见证有个没有解决的问题,mssql的主备是针对单个库的,有时候单个或多个库主备切换了,但是整个主数据库并没有挂掉,并且还运行着其他的库,程序检测到的数据库连接是正常的,只是部分库连接不了 ... 
- 学习java的第十五天
			一.今日收获 1.完成了手册第二章没有验证完成的例题 2.预习了第三章的算法以及for语句与if语句的用法 二.今日难题 1.验证上出现问题,没有那么仔细. 2.第二章还有没有完全理解的问题 三.明日 ... 
- Spark集群环境搭建——部署Spark集群
			在前面我们已经准备了三台服务器,并做好初始化,配置好jdk与免密登录等.并且已经安装好了hadoop集群. 如果还没有配置好的,参考我前面两篇博客: Spark集群环境搭建--服务器环境初始化:htt ... 
- Spring整合Ibatis之SqlMapClientDaoSupport
			前言 HibernateDaoSupport SqlMapClientDaoSupport . 其实就作用而言两者是一样的,都是为提供DAO支持,为访问数据库提供支持. 只不过HibernateD ... 
- spring注解-web
			以往进行web项目开发都需要在web.xml配置servlet.filter.listener,在Servlet3.0可以通过注解的方式配置它们(注意:必须用tomcat7以上版本) @WebServ ... 
- LR中的快捷建
			Ctrl+F 弹出搜索对话框 CTRL+F8 弹出view tree 界面 (寻找关联) 觉得不错的可关注微信公众号在手机上观看,让你用手机边玩边看 
- 一个简单的Extjs继承实现
			function extend(sub,sup){ //目地:实现只继承父类的原型对象 //1.用一个空函数据中转,目地进行中转 var F = new Function(); //2.实现空函数的的 ... 
