当前位置:网站首页>【二叉树-困难】124. 二叉树中的最大路径和
【二叉树-困难】124. 二叉树中的最大路径和
2022-08-10 01:52:00 【菜菜2022】
【题目】
【代码】
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def visit(self,root):
if not root:
return 0
l=max(self.visit(root.left),0)
r=max(self.visit(root.right),0)
self.cnt=max(self.cnt,l+r+root.val)
# print(root.val," l:",l," r:",r)
return root.val+max(l,r)
def maxPathSum(self, root: Optional[TreeNode]) -> int:
self.cnt=-30000000
self.visit(root)
return self.cnt
边栏推荐
猜你喜欢
Experimental support for decorators may change in future releases.Set the "experimentalDecorators" option in "tsconfig" or "jsconfig" to remove this warning
Sikuli's Automated Testing Technology Based on Pattern Recognition
微透镜阵列的高级模拟
墨西哥大众VW Mexico常见的几种label
HCIP——综合交换实验
Janus实际生产案例
2022强网杯 Quals Reverse 部分writeup
sqlmap dolog外带数据
Premint工具,作为普通人我们需要了解哪些内容?
xss的DOMPurify过滤框架:一个循环问题以及两个循环问题
随机推荐
首次在我们的centos上安装MySQL
The shell specifies the parameter name to pass the parameter
【web渗透】SSRF漏洞超详细讲解
Initial attempt at UI traversal
元素的盒子模型+标签的尺寸大小和偏移量+获取页面滚动距离
gbase 8a数据库如何查看数据或数据文件是否正常?
Summary of Web Performance Testing Models
多线程之享元模式和final原理
中英文互译在线翻译-在线翻译软件
Not, even the volume of the king to write code in the company are copying and pasting it reasonable?
Initial attempt at UI traversal
[Syntax sugar] About the mapping of category strings to category numeric ids
Open3D 泊松盘网格采样
16. 最接近的三数之和
Solve the problem of sed replacement text containing special characters such as "/" and "#"
C# rounding MidpointRounding.AwayFromZero
c# 解决CS8602告警 解引用可能出现空引用
已备案域名用国外服务器会不会掉备案?
【二叉树-中等】1104. 二叉树寻路
hopscotch game