Binary Tree Maximum Path Sum 自底向上求解(重重重重)
题目:
解答:
自底向上求解。left_max right_max分别返回了左右子树的最大路径和,假设左右子树最大路径和小于0。那么返回零。 用这个最大路径和和根节点的值相加。来更新最大值,同一时候。 更新返回该树的最大路径值。
代码:
class Solution {
public:
int max = INT_MIN;
int maxPathSum(TreeNode *root) {
if (root == NULL)
return 0;
search(root);
return max;
}
int search(TreeNode *root)
{
if (root == NULL)
return 0;
int left_max = search(root->left);
int right_max = search(root->right);
int sum = left_max + right_max + root->val;
if (sum > max)
max = sum;
sum = left_max > right_max ? left_max + root->val : right_max + root->val;
if (sum > 0)
return sum;
return 0;
}
};
Binary Tree Maximum Path Sum 自底向上求解(重重重重)的更多相关文章
- [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 ...
- 26. 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 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: 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
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 ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- [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. ...
随机推荐
- Python的前世今生
放弃 拾起 放弃 拾起 最终决定好好的编写一次Python的学习笔记 人生苦短,我用Python --------------Life is short, you need Python 再不抓紧,就 ...
- CentOS中一些基本的操作记录
1)切换到root su root 输入你的密码.我的是123
- Spring.Boot.1 -- 概览
Spring Boot 是如何简化Java 开发的 SpringBoot的一些重要特征 长久以来,Spring 框架作为Java应用开发的框架地位稳固.最近在云计算.大数据.无结构数据持续化.函数式反 ...
- eclipse之版本代号
- 安装配置elasticsearch、安装elasticsearch-analysis-ik插件、mysql导入数据到elasticsearch、安装yii2-elasticsearch及使用
一.安装elasticsearch 获取elasticsearch的rpm:wget https://download.elastic.co/elasticsearch/release/org/ela ...
- Linux中find命令用法大全
Linux 查找命令是Linux系统中最重要和最常用的命令之一.查找用于根据与参数匹配的文件指定的条件来搜索和查找文件和目录列表的命令.查找可以在各种条件下使用,您可以通过权限,用户,组,文件类型,日 ...
- Linux最常用的基础命令 上篇
Linux最常用的基础命令个人总结 计算机基础知识 32bit和64bit系统的区别.系统运行机制 1989年python 诞生 C语言是编译型的语言,不太支持跨平台 Django 江购 32bit= ...
- MyBatis 返回Map<String,Object>类型
<!-- 导出所有数据 --> <select id="exportAll" resultMap="map"> SELECT t1.ME ...
- VS2017 + Qt5 + OpenCV400 环境配置
首先为VS2017 IDE点赞. 配置核心 配置 Qt5 和 OpenCV400,最主要的就是头文件路径.库路径以及库文件名字. 配置方法和步骤 新建一个工程,或者打开一个已有的工程: 选择 View ...
- Altium designer中生成gerbera文件
在Altium designer中生成gerbera文件的方法有很多,不同版本,差异行不太大,正如下边链接地址里博主在10版本下的方法,在6.0版本长也是这样 http://blog.sina.com ...