[LeetCode]题解(python):124-Binary Tree Maximum Path Sum
题目来源:
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的更多相关文章
- 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 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 【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] 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 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 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 ...
随机推荐
- 07.19 Linux命令 cd
情景:在用compass编写sass,cd进入目录后,想退出, 解决: cd.. 回到上一层目录 cd\ 回到根目录 cd 进入具体目录
- mysql的查询缓存
查询是数据库技术中最常用的操作.查询操作的过程比较简单,首先从客户端发出查询的SQL语句,数据库服务端在接收到由客户端发来的 SQL语句后, 执行这条SQL语句,然后将查询到的结果返回给客户端 ...
- MVC多表联合查询数据显示
随然做过几年.net开发,但一直没有做过MVC框架下的网站,这段时间无事,学习一下.下面的方法是我摸索过程中的一点总结,如果有更好的方法,欢迎告诉我,谢谢. 这段时间我只看了MVC和LinQ两本书,关 ...
- Windows Server 2003 安装Sql Server 2005 问题处理
安装途中遇到: 问题1.无法找到产品Microsoft SQL Server Native Client的安装程序包.请使用安装包sqlncli.msi的有效副本重新安装? 答:安装SQL Serve ...
- T-SQL 基于关系分割字符串
今天晚上学习了下 SQL 基于关系的运算,同时也捉摸着写了个例子,虽然我知道性能不是很好,还有待优化.直接上源代码吧,思路表达出来有点困难,直接贴上代码,如果有人不懂得可以MM 我. declare ...
- Unity StrangeIoc框架 (一)
最近想项目中需要使用这个架构 因此 上网看了很多资料摸索 但是对于初学者来说大多数的资料不是那么容易理解 而且文档也是英文的阅读起来有点吃力 所以记录一下自己阅读的过程 方便以后翻阅和跟我一 ...
- BZOJ 1801: [Ahoi2009]chess 中国象棋( dp )
dp(i, j, k)表示考虑了前i行, 放了0个炮的有j列, 放了1个炮的有k列. 时间复杂度O(NM^2) -------------------------------------------- ...
- aJax学习之Ajax工作原理
转自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/18/2216553.html 在写这篇文章之前,曾经写过一篇关于AJAX技术的 ...
- IO流操作-图片操作(二)
一:Stream和Byte的含义 Stream:流,一般指文件流,内存流,可读的流和可写的流等,是一种数据转换的格式,流在计算机里可以表示为流输入或输出的一个连续的字节序列,它在使用完后,需要把资源释 ...
- Hadoop学习笔记01——Hadoop分布式文件系统
Hadoop有一个称为HDFS的分布式系统,全称为Hadoop Distributed Filesystem. HDFS有块(block)的概念,默认为64MB,HDFS上的文件被划分为块大小的多个分 ...