原题链接https://leetcode.com/problems/longest-univalue-path

题目描述

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.

Note: The length of path between two nodes is represented by the number of edges between them.

Example 1:

Input:

          5
/ \
4 5
/ \ \
1 1 5

Output:

2

Example 2:

Input:

          1
/ \
4 5
/ \ \
4 4 5

Output:

2

Note: The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.

思路分析

返回给定二叉树的相同节点值的最长路径,不一定要从根节点开始,且长度以节点之间的边数表示。
对于某个节点

  • 递归计算左右子树的最大长度
  • 更新当前最大长度
  • 若左右子树的根节点的值等于当前节点值的话,取大者
    All in all,很友好的题,但是我写题解&&做题的时间超时了……

AC解

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res = 0;
public int longestUnivaluePath(TreeNode root) {
if(root == null)
{
return 0;
} dfs(root,0);
return res;
} public int dfs(TreeNode root,int curVal)
{
if(root == null)
{
return 0;
} int left = (root.left != null) ? dfs(root.left,root.val) : 0;
int right = (root.right != null) ? dfs(root.right,root.val) : 0; res = Math.max(res,left + right); return (curVal == root.val) ? (Math.max(left,right)+1) : 0;
}
}

Longest Univalue Path——LeetCode进阶路的更多相关文章

  1. 【LeetCode】687. Longest Univalue Path 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  2. 【Leetcode_easy】687. Longest Univalue Path

    problem 687. Longest Univalue Path 参考 1. Leetcode_easy_687. Longest Univalue Path; 2. Grandyang; 完

  3. LC 687. Longest Univalue Path

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  4. [LeetCode] Longest Univalue Path 最长相同值路径

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  5. [LeetCode] 687. Longest Univalue Path 最长唯一值路径

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  6. LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)

    题目: Given a binary tree, find the length of the longest path where each node in the path has the sam ...

  7. LeetCode算法题-Longest Univalue Path(Java实现)

    这是悦乐书的第290次更新,第308篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第158题(顺位题号是687).给定二叉树,找到路径中每个节点具有相同值的最长路径的长度 ...

  8. leetcode 687.Longest Univalue Path

    寻找最长的路径,那么会在左边或者右边或者是从左到跟然后再到右方的路径的. /** * Definition for a binary tree node. * struct TreeNode { * ...

  9. [Swift]LeetCode687. 最长同值路径 | Longest Univalue Path

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  10. Recursion-687. Longest Univalue Path

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

随机推荐

  1. php执行时间

    要计算代码的bai执行时间,在PHP来讲是du十分简单的,首先,zhi你需要知道,PHP是一种dao顺序执行的脚本语言,所以,可以按照以下步骤来计算代码的执行时间: <?php function ...

  2. C语言格式输出方式

    C语言格式输出 1.转换字符说明 C语言格式输出方式 2.常用的打印格式 在 C 语言中,格式输出主要依靠 printf 函数来实现. 以下是一些 C 语言格式输出的代码举例及相关说明: printf ...

  3. 【数值计算方法】线性方程组迭代算法的Python实现

    线性方程组迭代算法的Python实现 jacobi,GS,SOR迭代法 def JacobiIter(A:np.ndarray, b:np.ndarray, tol:float=1e-5, maxIt ...

  4. 使用UNRAID系统,搭建ALL IN ONE全过程

    NAS最强攻略:使用UNRAID系统,搭建ALL IN ONE全过程!超万字教程,绝对干货! 2020-06-01 11:24:27 1690点赞 11149收藏 717评论 创作立场声明:熬了几个通 ...

  5. [源码系列:手写spring] IOC第七节:加载xml文件中定义的Bean

    目录 主要内容 代码分支 核心代码 BeanDefinitionReader AbstractBeanDefinitionReader XmlBeanDefinitionReader 测试 bean定 ...

  6. Code First 初始化数据时发生异常

    问题重现 用Entity Framework的Code First默认生成的数据库文件被我直接删除了, 然后不管怎么重新编译等等, 运行后总是会报错如下: 解决方案同下 Cannot attach t ...

  7. HTTPS方案浅谈

    付费方案 赛门铁克 沃通 其他...懒得看了,重点不是这些 免费方案 WoSign(沃通)的DV免费SSL证书: 免费SSL证书支持最多5个域名, 一次性可管2年, 到期后可免费续期,相当于永久免费. ...

  8. 学习Kotlin语法(三)

    简介 在上一节,我们对Kotlin中面向对象编程(OOP)的相关知识有了大致的了解,本章节我们将去进一步了解函数.lambada表达式.内联函数.操作符重载.作用域函数. 目录 函数 函数的使用 参数 ...

  9. 详细介绍Mybatis的缓存机制

    一.缓存机制 1.缓存概述 缓存:缓存就是一块内存空间,保存临时数据 作用:将数据源(数据库或者文件)中的数据读取出来存放到缓存中,再次获取时直接从缓存中获取,可以减少和数据库交互的次数,提升程序的性 ...

  10. AQS的release(int)方法底层源码

    一.定义 release(int) 是 AQS(AbstractQueuedSynchronizer)中的一个核心方法,用于在独占模式下释放同步状态.如果释放成功,则会唤醒等待队列中的后继节点,使其有 ...