原题链接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. 2. 在Linux 当中安装 Nginx(13步) 下载&安装&启动(详细说明+附加详细截图说明)

    2. 在Linux 当中安装 Nginx(13步) 下载&安装&启动(详细说明+附加详细截图说明) @ 目录 2. 在Linux 当中安装 Nginx(13步) 下载&安装&a ...

  2. AXUI前端框架v3版本已经发布,底层完全改写,基于原生技术标准,想走得更远!

    AXUI的v3版本已经发布! AXUI框架已经经历了第一代和第二代的迭代,充分认识到纯CSS和HTML的局限性,也意识到过多手动编写代码会影响用户体验.因此,AXUI的目标是:既满足原生前端的标准,又 ...

  3. New Bing 全面开放?我看未必

    前段时间大家应该都被ChatGPT刷屏了,其实就回答来说New Bing 才是最厉害的,因为它底层使用了ChatGPT 并且可以支持联网查询数据,回答中还能支持看到出处,方便确认其真实性. New B ...

  4. nginx 禁止直接访问目录或文件

    前言 Nginx 默认是不允许列出整个目录的. 如需此功能,打开 nginx.conf 文件或你要启用目录浏览虚拟主机的配置文件,在 location server 或 http 段中加入 autoi ...

  5. harbor

    一篇带你了解私有仓库 Harbor 的搭建 一.Harbor简介 虽然Docker官方提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署我们私有环境内的Registry也是非常必要的. Harbo ...

  6. Netty源码—4.客户端接入流程

    大纲 1.关于Netty客户端连接接入问题整理 2.Reactor线程模型和服务端启动流程 3.Netty新连接接入的整体处理逻辑 4.新连接接入之检测新连接 5.新连接接入之创建NioSocketC ...

  7. React Props指南:从基础到高阶应用的最佳实践解析

    在 React 中,Props(属性)是组件间通信和数据传递的核心机制.通过合理使用 Props,开发者可以构建动态.可复用且易于维护的组件体系.本文将深入探讨 Props 的核心概念.使用方法及最佳 ...

  8. C#(如何解决使用enum和struct作为Dictionary的TKey带来的GC

  9. ThreadPoolExecutor的内部类Worker详细解析

    一.定义 ThreadPoolExecutor 的内部类 Worker 是线程池的核心实现之一,它封装了线程和任务,并负责执行任务.Worker 类继承自 AbstractQueuedSynchron ...

  10. Debug调试(使用IDEA的断点调试功能,查看程序的运行过程)

    一. 1. 在有效代码行,点击行号右边的空白区域,设置断点,程序执行到断点将停止,我们可以手动来运行程序 2. 点击Debug运行模式 3. 程序停止在断点上不再执行,而IDEA最下方打开了Debug ...