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. ...
随机推荐
- [6818开发板]八核开发板|4G开发板|GPS开发板|嵌入式开发平台
IMX6开发板(基本型):960元 IMX6开发板(豪华型):1460元 S5P4418 核心板可以无缝支持核心系统S5P6818,并保持底板设计不变,将兼顾更高端 的应用领域,为项目和产品提供更好的 ...
- css 最高权重 !important;
border-top: 1px solid #ccc !important;
- Ubuntu16.04 python3.4.3升级到python3.7.1
python有两个版本,一个2版本,使用的是python:另一个是3版本,使用的是python3. 简易安装python后得到的3版本的版本号是python3.4.3. 可以使用下面的命令查看py版本 ...
- windows下载安装mysql
一.下载mysql 1.下载地址 https://www.mysql.com/downloads/ 2.选择windows,如图 3.点击MySQL Install 4.现在版本是8.0.16,在弹出 ...
- python3+beautifulSoup4.6抓取某网站小说(二)基础功能设计
本章学习内容:1.网页编码还原读取2.功能设计 stuep1:网页编码还原读取 本次抓取对象: http://www.cuiweijuxs.com/jingpinxiaoshuo/ 按照第一篇的代码来 ...
- DataRow复制一行到另一个DataTable
DataRow复制一行到另一个DataTable 下面两个方法是DataRow复制一行到另一个DataTable的,直接Add会出错“此行已属于另一个表”,其实以前就知道怎么做的,可每次要用到的时 ...
- cacheStorage缓存及离线开发
案例地址:https://zhangxinxu.github.io/https-demo/cache/start.html 我们直接看一个例子吧,如下HTML和JS代码: <h3>一些提示 ...
- ionic3 ion-slides遇坑
不想吐槽 ionic-slides 的组件,是个巨坑...切换页面以后再返回当前页面, 不能自动播放,网上的解决方案都是没用的(亲测,后台获取的数据) ... 不信邪的宝宝们可以去试试..建议换 ...
- js中sync、defer、async的区别
<script src="script.js"></script> 没有 defer 或 async,浏览器会默认为同步sync,会立即加载并执行指定的脚本 ...
- Codeforces Beta Round #93 (Div. 2 Only) (Virtual participation)
A 相邻点对距离和*k B (Σ(v/2))/2 C 一直想不到"最优"是怎么体现的,发现y2=y1*(t1-t0)/(t0-t2),就写了1e6的枚举,然而又一些特殊情况没考虑到 ...