题目来源:

  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. DZY的根(思维水)

    DZY的根[问题描述]DZY是个没有根的人,他十分想要有根,这样才能...智商爆表的计算机大神WJC决定再帮帮他,他用程序制造了N个根——有生命的根!这N个根和WJC一样都十分机智,他们要参加国际象棋 ...

  2. 《4》CentOS7.0+OpenStack+kvm云平台部署—配置Nova

    感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  3. docker private registry使用

    一.搭建harbor: 步骤:略 二.命令行操作: 登录:docker login docker01 tag image: tag 一个 image,名称一定要标准( registryAddress[ ...

  4. 用js获取周、月第一天和最后一天(转载)

    var getCurrentWeek = function (day) { var days = ["周日", "周一", "周二", &q ...

  5. A - A

    A - A Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status ...

  6. C++学习之函数指针

     C++学习之函数指针          和数据项类似,函数也有地址,函数的地址是存储在机器语言代码的内存的开始地址.通常,这些地址对用户而言,不重要也没什么用处,但对程序而言,它却很有用. 一.函数 ...

  7. 【转】Eclipse自动补全的设置方法

    转自:http://blog.csdn.net/xiadasong007/archive/2009/11/11/4799715.aspx 打开 Eclipse -> Window -> P ...

  8. 《Pointers On C》读书笔记(第三章 数据)

    1.在C语言中,仅有4种基本数据类型:整型.浮点型.指针和聚合类型(如数组和结构等). 整型家族包括字符.短整型.整型和长整型,它们都分为有符号和无符号两种. 标准规定整型值相互之间大小的规则:长整型 ...

  9. http 响应头之location

    <pre name="code" class="html">jrhmpt01:/root# cat login_yylc.pl use LWP::U ...

  10. nat123外网SSH访问内网LINUX的N种方法

    一,动态公网IP环境 1,环境描述: 路由器分配的是动态公网IP,且有路由管理权限,LINUX主机部署在路由内网.如何实现外网SSH访问内网LINUX主机? 2,解决方案: 使用nat123动态域名解 ...