687. Longest Univalue Path文章来源:https://www.toymoban.com/news/detail-805415.html
# 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 depth(self,root):
if root==None:return 0
r,l=self.depth(root.right),self.depth(root.left)
if root.left:
if root.val!=root.left.val:l=0
else:l+=1
if root.right:
if root.val!=root.right.val:r=0
else:r+=1
self.ans=max(self.ans,l+r)
return max(l,r)
def longestUnivaluePath(self, root: Optional[TreeNode]) -> int:
self.ans=0
self.depth(root)
return self.ans
l,r别写错文章来源地址https://www.toymoban.com/news/detail-805415.html
到了这里,关于687. Longest Univalue Path的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!