题目来源:

  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. Billboard(线段树)

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. Kali linux安装漏洞扫描工具Nessus指南

    引子:Nessus是著名信息安全服务公司tenable推出的一款漏洞扫描与分析软件,号称是"世界上最流行的漏洞扫描程序,全世界超过75,000个组织在使用它".虽然这个扫描程序能够 ...

  3. Android牛博

    Android实现伸缩弹力分布菜单效果 摘要: 本文介绍下在Android中实现伸缩弹力分布菜单效果.关于这种菜单效果在IPhone中比较常见,效果比较酷.那么在Android中实现只是一种简单的模仿 ...

  4. Ceph对象存储网关中的索引工作原理<转>

    Ceph 对象存储网关允许你通过 Swift 及 S3 API 访问 Ceph .它将这些 API 请求转化为 librados 请求.Librados 是一个非常出色的对象存储(库)但是它无法高效的 ...

  5. openvswitch安装、基本操作

    一.安装,配置 //下载源码.编译.安装: #wget http://openvswitch.org/releases/openvswitch-2.3.0.tar.gz #tar -zxvf open ...

  6. SQL练习之不破坏应用程序现有查询的修改模式

    当我还是一个菜鸟的时候,当然现在也是,当我的软件需求发生变化时,并且数据库设计同样要求发生变化,我通常会放弃原有的代码(或者对原有的代码进行大改),先在我知道了两个不破坏应用程序现有查询的修改模式,下 ...

  7. python 备份脚本

    import osimport timesource= r"out_res.txt"target_dir= r"F:\python\Doc"target=tar ...

  8. Qt(QML)本地化

    Internationalization and Localization with Qt Quick 程序国际化 1) Use qsTr() for all  Literial UI strings ...

  9. textarea中的空格与换行

    当在一个textarea标签中键入一个回车时,实际上会插入2个符号:\n\r在javascript里, line breaks用\n表示when you pull text into Javascri ...

  10. 替换 window.location当中的某个参数的值(而其它值不变)JS代码

    在后台与前台的交互操作中,需要替换location当中的某个参数的值(而其它值不变)时,会用到以下函数: 说明: win:传入的窗口句柄,如,window或window.parent等forceAdd ...