当前位置:网站首页>【二叉树-中等】687. 最长同值路径
【二叉树-中等】687. 最长同值路径
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
l=self.visit(root.left)
r=self.visit(root.right)
l2,r2=0,0
if root.left and root.left.val==root.val:
l2=l+1
if root.right and root.right.val==root.val:
r2=r+1
self.cnt=max(self.cnt,l2+r2)
# print(root.val," l:",l," r:",r)
return max(l2,r2)
def longestUnivaluePath(self, root: Optional[TreeNode]) -> int:
self.cnt=0
self.visit(root)
return self.cnt
边栏推荐
猜你喜欢
随机推荐
c# 解决CS8602告警 解引用可能出现空引用
Golang nil的妙用
FusionConpute虚拟机的发放与管理
小程序开发的报价为什么有差别?需要多少钱?
[QNX Hypervisor 2.2用户手册]10.14 smmu
【UNR #6 B】机器人表演(DP)
Janus actual production case
Linux(Centos7)服务器中配置Mysql主从数据库,以及数据库的安装,防火墙操作
Screen 拆分屏幕
桌面云组件介绍与安装
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
Initial attempt at UI traversal
OpenCV图像处理学习一,加载显示修改保存图像相关函数
OpenSSF的开源软件风险评估工具:Scorecards
【引用计数器及学习MRC的理由 Objective-C语言】
卷积神经网络识别验证码
Solve the problem of sed replacement text containing special characters such as "/" and "#"
Unity3D创建道路插件EasyRoads的使用
SonarQube升级记录:7.8->7.9->8.9
如何让数据库中的数据同步