LeetCode 124. 二叉树中的最大路径和(Binary Tree Maximum Path Sum)
题目描述
给定一个非空二叉树,返回其最大路径和。
本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。
示例 1:
输入: [1,2,3] 1
/ \
2 3 输出: 6
示例 2:
输入: [-10,9,20,null,null,15,7] -10
/ \
9 20
/ \
15 7 输出: 42
解题思路
利用后序遍历的思想,先分别求出左右子树中由根节点发出的最大路径,然后记录当前的最大路径和为根节点加左右路径和的最大值,再返回当前子树由根节点发出的最大路径和。
代码
/**
* 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 findPath(TreeNode* root, int &maxSum){
int left = , right = , val = root->val;
if(root->left){
left = findPath(root->left, maxSum);
if(left > ) val += left;
}
if(root->right){
right = findPath(root->right, maxSum);
if(right > ) val += right;
}
maxSum = max(maxSum, val);
return max(root->val, root->val + max(left, right));
}
int maxPathSum(TreeNode* root) {
if(root == NULL) return ;
int maxSum = INT_MIN;
findPath(root, maxSum);
return maxSum;
}
};
LeetCode 124. 二叉树中的最大路径和(Binary Tree Maximum Path Sum)的更多相关文章
- [Swift]LeetCode124. 二叉树中的最大路径和 | 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 ...
- 二叉树中的最大路径和 · Binary Tree Maximum Path Sum
[抄题]: 给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和) [思维问题]: 不会写分合法 [一句话思路]: 用两次分治:ro ...
- 二叉树最大路径和-Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- 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】124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- [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 ...
- 【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 ...
- Java实现 LeetCode 124 二叉树中的最大路径和
124. 二叉树中的最大路径和 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: ...
随机推荐
- 15 Django之Celery发送邮件
异步任务--celery发送邮件 安装两个python包: pip install celery==3.1.25 pip install django-celery==3.2.1 pip instal ...
- 重拾MVC——第一天:数据库连接与SqlDbHelper
这个 SqlDbHelper 是我参考网上的和以前用过的 SqlDbHelper 自己写的一个非常简单的东西,主要是记录自己的学习情况 首先在Web.config中配置数据库连接字符串: <co ...
- scroll滚动条掩藏
掩藏scroll滚动条::-webkit-scrollbar ::-webkit-scrollbar {} /* 1 */ ::-webkit-scrollbar-button {} /* 2 */ ...
- 组合:abc三个字符的所有组合
求所有组合也就是abc各个位是否选取的问题,第一位2中可能,第二位2种...所以一共有2^n种.用0表示不取,1表示选取,这样可以用110这样的形式表示ab.abc一共的表示形式从0到2^3-1.然后 ...
- seaborn图形
kdeplot(核密度估计图) 核密度估计(kernel density estimation)是在概率论中用来估计未知的密度函数,属于非参数检验方法之一.通过核密度估计图可以比较直观的看出数据样本本 ...
- Django 权限控制配置步骤
1.models下面添加权限控制信息: class UserProfile(models.Model): user = models.OneToOneField(User) name = models ...
- 解决Python中出现的问题: “You are using pip version 9.0.1, however version 19.2.3 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.”
1. 一开始我在使用Pycharm时,导入numpy库,发现导入错误: Non-zero exit code (1) 2. 于是我通过更新的方法来解决,哪知道在更新的时候也出现了错误,错误如下图: 这 ...
- Linux初始化脚本
以下脚本用于linux系统的初始化脚本,可以在服务器系统安装完毕之后立即执行.脚本结合各位大牛一些参数,已经在CentOS 5下通过. 使用方法:将其复制,保存为一个shell文件,比如init.sh ...
- laravel常用响应操作
- A Hands-on Look at Using Ray Tracing in Games with UE 4.22 GDC 2019
A Hands-on Look at Using Ray Tracing in Games with UE 4.22 GDC 2019 talker: Sjoerd De Jong (SR.ENGIN ...