题目地址: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;
};

相似题目

543. Diameter of Binary Tree

日期

2019 年 9 月 27 日 —— 昨天面快手,竟然是纯刷题

【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)的更多相关文章

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. git冲突

    常规操作: # quan @ quandeMacBook-Pro in ~/Desktop/10-01/app_addressing----quan on git:1.0.1.Release o [1 ...

  2. bcftools 提取vcf(snp/indel)文件子集

    做群体变异检测后,通常会有提取子集的操作,之前没有发现bcftools有这个功能,都是自己写脚本操作,数据量一上来,速度真的是让人无语凝噎.这里记录下提取子vcf文件的用法,软件版本:bcftools ...

  3. Macbook pro进入恢复模式以及无法进入恢复模式解决方案

    看网上很多说用Command+R进入恢复模式,但是,大部分都反馈说,此命令并不能进入恢复模式.我自己也尝试发现了同样问题,最终发现解决方案: 问题出在,[是重新启动电脑,而不是关机+按开机键,否则会造 ...

  4. ubuntu终端颜色快速配置

    ubuntu终端颜色快速配置 根据以下step步骤设置即可 step1:备份:cp ~/.bashrc ~/.bashrc.backup step2:打开文件:vim ~/.bashrc step3: ...

  5. vue-baidu-map相关随笔

    一,使用vue-baidu-map 1.下载相关包依赖 npm i vue-baidu-map   2.在main.js中import引入相关包依赖,在main.js中添加如下代码: import B ...

  6. 从源码看Thread&ThreadLocal&ThreadLocalMap的关系与原理

    1.三者的之间的关系 ThreadLocalMap是Thread类的成员变量threadLocals,一个线程拥有一个ThreadLocalMap,一个ThreadLocalMap可以有多个Threa ...

  7. Templates and Default Arguments

    Default parameters for templates in C++: Like function default arguments, templates can also have de ...

  8. spring-cloud-alibaba-dependencies版本问题

    org.springframework.cloud的spring-cloud-alibaba-dependencies管理的nacos最新版本是0.9.0.RELEASE,已经不再维护了,用起来有版本 ...

  9. Centos7源码部署Redis3.2.9

    目录 一.环境准备 二.安装 三.测试 四.编写启动脚本 一.环境准备 [Redis-Server] 主机名 = host-1 系统 = centos-7.3 地址 = 1.1.1.1 软件 = re ...

  10. zctf_2016_note3(unlink)

    这道题完全没想到漏洞在哪(还是菜了) 这道题目我通过海哥的博客学习的 (16条消息) zctf_2016_note3_seaaseesa的博客-CSDN博客 例行检查我就不放了 进入edit页面 这里 ...