687. Longest Univalue Path

这篇具有很好参考价值的文章主要介绍了687. Longest Univalue Path。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

687. Longest Univalue Path

# 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模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode!! 3. Longest Substring Without Repeating Characters

    参考资料:左程云算法课 思路: 首先,以str[i]为例,计算 局部最优解 以str[i]为结尾的无重复字符的最长子串的长度 。然后,遍历字符串,即i取0,1,…, n-1,取局部最优解的最大值,得到全局最优解。 具体地,求 以str[i]为结尾的无重复字符的最长子串的长度 ,可以使用动

    2024年02月08日
    浏览(25)
  • 【递增栈】个人练习-Leetcode-845. Longest Mountain in Array

    题目链接:https://leetcode.cn/problems/longest-mountain-in-array/ 题目大意:给出一个数组 arr[] ,定义【山数组】为【长度大于等于3】【前面部分递增,后面部分递减,存在一个山峰元素】的数组。求 arr[] 中的最长的是【山数组】的子列。 思路:递增栈当然可以,不过刚好想到另一个

    2024年02月06日
    浏览(41)
  • LeetCode 1048. Longest String Chain【记忆化搜索,动态规划,哈希表,字符串】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月05日
    浏览(33)
  • LeetCode //C - 71. Simplify Path

    Given a string path, which is an absolute path (starting with a slash ‘/’) to a file or directory in a Unix-style file system, convert it to the simplified canonical path. In a Unix-style file system, a period ‘.’ refers to the current directory, a double period ‘…’ refers to the directory up a level, and any multiple consecutive slashes (i.e. ‘

    2024年02月12日
    浏览(31)
  • LeetCode //C - 124. Binary Tree Maximum Path Sum

    A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root. The path sum of a path is the sum of the node’s values in the path. Given the root of a binary tree, return the maximum

    2024年02月09日
    浏览(31)
  • LeetCode --- 1971. Find if Path Exists in Graph 解题报告

    There is a  bi-directional  graph with  n  vertices, where each vertex is labeled from  0  to  n - 1  ( inclusive ). The edges in the graph are represented as a 2D integer array  edges , where each  edges[i] = [ui, vi]  denotes a bi-directional edge between vertex  ui  and vertex  vi . Every vertex pair is connected by  at most one  edge, and

    2024年02月07日
    浏览(33)
  • leetcode - 1293. Shortest Path in a Grid with Obstacles Elimination

    You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You can move up, down, left, or right from and to an empty cell in one step. Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right corner (m - 1, n - 1) given that you can eliminate at most k obstacles. If it is not possib

    2024年02月08日
    浏览(34)
  • 深度优先搜索与动态规划|543, 124, 687

    好久没写二叉树了,主要还是看遍历的顺序是什么样的。 这个有点绕不出来。绕一遍, root -10 进去,root.left是root 9,root.right是root 20 root 9 进去得到的return是9,res更新得到9 root 20进去,root.left是root 15,root.right是root 7 root 15进去得到的return是15,res更新得到15 root 7进去得到的

    2024年02月13日
    浏览(32)
  • 微软使用“钞能力”: 687 亿美元收购动视暴雪!

    就在昨晚,刚准备睡觉,就看到新闻:微软宣布收购动视暴雪! 动视暴雪!这可是大家的青春啊,星际争霸、暗黑破坏神、魔兽争霸、魔兽世界、炉石传说、守望先锋等,诸多风靡全球的游戏均出自暴雪之手。 微软称,将以 每股95美元的价格全现金收 购动视暴雪,总交易价

    2024年02月08日
    浏览(29)
  • 索尼慌了?微软687亿美元收购暴雪,为元宇宙做打算?

    1月18日, 微软同意以687亿美元收购视频游戏制造商动视暴雪,这成为微软公司有史以来最大的一笔交易, 这项交易也是微软历来的最大手笔收购,交易完成后,微软将成为按营收计算的全球第三大游戏公司,仅次于腾讯和索尼。   爱过、骂过,谁的青春没有动视暴雪? 稍有

    2024年02月04日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包