当前位置:网站首页>【二叉树-中等】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
边栏推荐
- 【二叉树-中等】1104. 二叉树寻路
- 墨西哥大众VW Mexico常见的几种label
- Research on Ethernet PHY Chip LAN8720A Chip
- [Syntax sugar] About the mapping of category strings to category numeric ids
- C# 正则表达式分组查询
- 在蓝图中给组件动态加子Actor组件
- The shell specifies the parameter name to pass the parameter
- 月薪35K,靠八股文就能做到的事,你居然不知道
- 基于C51的中断控制
- FusionConpute虚拟机的发放与管理
猜你喜欢

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

color socks problem

Shader Graph learns various special effects cases

数组(一)

Database management tool: dynamic read-write separation

Initial attempt at UI traversal

【二叉树-中等】1379. 找出克隆二叉树中的相同节点

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

自动化测试中,测试数据与脚本分离以及参数化方法

Problems and solutions related to Chinese character set in file operations in ABAP
随机推荐
如何让数据库中的数据同步
Chip Information|Semiconductor revenue growth expected to slow to 7%, Bluetooth chip demand still growing steadily
[Swoole Series 3.5] Process Pool and Process Manager
C# winform 单选框
使用IDEA的PUSH常见问题
781. 森林中的兔子
hopscotch game
FILE结构体在stdio.h头文件源码里的详细代码
web开发概述
网络爬虫错误
数据库治理利器:动态读写分离
Deep Learning (5) CNN Convolutional Neural Network
16. 最接近的三数之和
【QT】QT项目:自制Wireshark
【web渗透】SSRF漏洞超详细讲解
2022年8月8日-2022年8月15日,ue4视频教程+插件源码()
Summary of Web Performance Testing Models
one of the variables needed for gradient computation has been modified by an inplace
Database management tool: dynamic read-write separation
【每日一题】1413. 逐步求和得到正数的最小值