题目地址: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. 61. Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal My Submissions QuestionEditorial Solution Total Accepted: 123484 Total ...

  2. 2.MaxSubArray-Leetcode

    题目:最大连续子序列和 思路:动态规划 状态转移方程 f[j]=max{f[j-1]+s[j],s[j]}, 其中1<=j<=n target = max{f[j]}, 其中1<=j ...

  3. UE4打包启动失败:RunUAT.bat ERROR: AutomationTool failed to compile.

    打包配置正常的情况下,出现下面Log: RunUAT.bat ERROR: AutomationTool failed to compile. 基本上,可以先排查下任务管理器中是不是有UE4Edito ...

  4. 使用 Skywalking 对 Kubernetes(K8s)中的微服务进行监控

    1. 概述 老话说的好:任何成功都不是轻易得来的,是不断地坚持与面对的结果. 言归正传,之前我们聊了 SpringCloud 开发的微服务是如何部署在  Kubernetes(K8s)集群中的,今天我 ...

  5. C++ 中的多重继承的问题

    如何正确使用C++多重继承 BY R12F · PUBLISHED 2011年06月17日 · UPDATED 2012年03月11日   原创文章,转载请注明:转载自Soul Apogee本文链接地 ...

  6. admire, admit

    admire 当别人admire你时,小心掉进泥潭(mire).词源:to wonder. wonderful夸人,awful骂人,awesome夸人.admiral与admire词源不同,碰巧长得像 ...

  7. Linux学习 - 变量测试与内容替换

    变量置换方式 变量y没有设置 变量y为空 变量y有值 x=${y-新值} x=新值 x空 x=$y x=${y:-新值} x=新值 x=新值 x=$y x=${y+新值} x空 x=新值 x=新值 x ...

  8. linux 配置本地yum

    1.挂载光盘 #挂载光盘 mount /dev/cdrom /mnt/cdrom 2.修改yum.conf, 运行 vi /etc/yum.conf,文件替换成如下内容 [main] cachedir ...

  9. class.getName()和class.getSimpleName()的区别

    根据API中的定义: Class.getName():以String的形式,返回Class对象的"实体"名称: Class.getSimpleName():获取源代码中给出的&qu ...

  10. JS - 获取当前的时间,并且转换成年 - 月 - 日格式!

    先获取当前时间,并转换成年月日格式! function getNowFormatDate() { var date = new Date(); var seperator1 = "-&quo ...