当前位置:网站首页>【二叉树-中等】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
边栏推荐
猜你喜欢

FILE结构体在stdio.h头文件源码里的详细代码

多线程之自定义线程池
![[网鼎杯 2020 青龙组]AreUSerialz](/img/33/a237185ffe0c5780432c242c36cbdc.png)
[网鼎杯 2020 青龙组]AreUSerialz

Unity reports Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe’ code” in Pla

Under pressure, there must be cowards

Shader Graph learns various special effects cases

墨西哥大众VW Mexico常见的几种label

用于X射线光学器件的哈特曼波前传感器

Maya制作赛博朋克机器人模型

已备案域名用国外服务器会不会掉备案?
随机推荐
【论文笔记】基于深度学习的机器人抓取虚拟仿真实验教学系统
多线程之自定义线程池
Initial attempt at UI traversal
【每日一题】1413. 逐步求和得到正数的最小值
sql实战积累
Janus actual production case
LeetCode 每日一题——1413. 逐步求和得到正数的最小值
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
自动化测试中,测试数据与脚本分离以及参数化方法
具有多孔光纤的偏振分束器
web开发概述
【二叉树-中等】1379. 找出克隆二叉树中的相同节点
Unity reports Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe’ code” in Pla
Unity editor extension interface uses List
Unity vertex animation
数据治理(五):元数据管理
【引用计数器及学习MRC的理由 Objective-C语言】
力扣每日一题-第51天-744. 寻找比目标字母大的最小字母
【内存管理概述 Objective-C语言】
C# 单例模式