题目来源:

  https://leetcode.com/problems/binary-tree-maximum-path-sum/


题意分析:

  给定一棵树,找出一个数值最大的路径,起点可以是任意节点或者叶子。


题目思路:

  我们可以先找路径的最大mr,ml,那么最大值是max(solve(root),solve(left),solve(right), max(mr + root.val + ml, root.val))。


代码(python):

  

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def maxPathSum(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root == None:
return 0
Solution.maxsum = root.val
def solve(root):
if root == None:
return 0
sum,l,r = root.val,0,0
if root.left:
l = solve(root.left)
if l > 0: sum += l
if root.right:
r = solve(root.right)
if r > 0: sum += r
Solution.maxsum = max(Solution.maxsum,sum)
#print(Solution.maxsum)
return max(root.val,max(root.val + l,root.val+r))
solve(root)
return Solution.maxsum

[LeetCode]题解(python):124-Binary Tree Maximum Path Sum的更多相关文章

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

  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. Leetcode solution 124: Binary Tree Maximum Path Sum

    Problem Statement Given a non-empty binary tree, find the maximum path sum. For this problem, a path ...

  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 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  10. 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. 学习使用React Native的心得体会

    首先根据官网上的介绍,安装必须的环境需求.http://reactnative.cn/docs/0.20/getting-started.html#content 下面讲一下一些常用的命令: .下载n ...

  2. JavaScript之共享onload

    我们知道,当我们将JS代码脚本放到<head></head>标签之间时,这是的js代码加载要先于DOM加载,而我们往往会在JS代码脚本中写一些获取DOM元素的代码,而此时的DO ...

  3. 块对象block小结

    blcok的形式 ^(参数列){主体} block作为返回值

  4. C# WINFORM 线程中更新UI

    幸好今天是周末,有时间把这个问题记录一下.在多种语言之间切换,发现开发效率降的很低了,开发成本都集中到调式上了,C/C++这些放弃很久了,突然感觉线程这个问题搞的有点烦躁 我这里提到的线程中更新UI, ...

  5. 为什么要for循环以及for循环的流程

    /* Name:为什么需要循环以及for循环流程 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月10日 03:16:55 Description:求1 ...

  6. JS 节流阀

    JS 节流阀 参考 https://github.com/hahnzhu/read-code-per-day/issues/5 节流阀 节流阀的基本原理 事件函数的执行都记下当前时间, 只有当前时间与 ...

  7. Oracle字符集转换

            这几天在工作中碰到一个字符乱码的问题,发现在cmd窗口的sqlplus中直接update一个中文和使用@调用一个文件作同样更新的时候,存储的结果 竟不一样.一时比较迷惑,对Oracle ...

  8. HDU 5755 Gambler Bo(高斯消元)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5755 [题目大意] 一个n*m由0,1,2组成的矩阵,每次操作可以选取一个方格,使得它加上2之后对 ...

  9. BZOJ 1096 [ZJOI2007]仓库建设(斜率优化DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1096 [题目大意] 有个斜坡,有n个仓库,每个仓库里面都有一些物品,物品数目为p,仓库 ...

  10. Git安装及基本使用

    准备: Git软件,github账号. Git安装: 直接百度搜git下载,windows和mac不同平台的.官网上的下载地址很慢或者根本下不了. 默认配置安装. github: 网址:https:/ ...