题目:

链接

解答:

自底向上求解。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 自底向上求解(重重重重)的更多相关文章

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

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

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

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

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

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

  7. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

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

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

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

随机推荐

  1. 数据层优化-jdbc连接池简述、druid简介

    终于回到既定轨道上了,这一篇讲讲数据库连接池的相关知识,线程池以后有机会再结合项目单独写篇文章(自己给自己挖坑,不知道什么时候能填上),从这一篇文章开始到本阶段结束的文章都会围绕数据库和dao层的优化 ...

  2. ZXing.dll 生成二维码 C# winform net4.5

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. Windows:32位程序运行在64位系统上注册表会重定向

    参考资料 微软注册表英文文档 StackOverflow社区回答 1.注册表位置 64bit系统(Windows Server 2008 R2只有64bit系统)的注册表分32 位注册表项和64位注册 ...

  4. Oracle数据库存储过程练习20181212

    先创建一个测试的数据表 --测试表 CREATE TABLE TEST20181207 ( ID INTEGER PRIMARY KEY, FUND NUMBER,--上日资金 BALANCE NUM ...

  5. 08CSS边框边距

    CSS边框边距 边框样式——border-style border-top-style border-bottom-style border-left-style border-right-style ...

  6. vue多视图

    第一步   在app.vue中 <router-view class="b" name="header"> </router-view> ...

  7. vue 改变插值方法

    Vue默认的插值是双大括号{{}}.但有时我们会有需求更改这个插值的形式. delimiters:['${','}']  //必须要用一个数组来接收,用逗号隔开.

  8. Go:struct

    一.使用方式 方式3和方式4返回的是:结构体指针,编译器底层对 p.Name 做了转化 *(p).Name,方便程序员使用. type Person struct { Name string Age ...

  9. js 技巧 (一)

      · 事件源对象 event.srcElement.tagName event.srcElement.type · 捕获释放event.srcElement.setCapture();  event ...

  10. find -print0和xargs -0原理及用法

    平常我们经常把find和xargs搭配使用,例如: find . -name "*.txt" | xargs rm 但是这个命令如果遇到文件名里有空格或者换行符,就会出错.因为xa ...