Longest Univalue Path——LeetCode进阶路
原题链接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进阶路的更多相关文章
- 【LeetCode】687. Longest Univalue Path 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- 【Leetcode_easy】687. Longest Univalue Path
problem 687. Longest Univalue Path 参考 1. Leetcode_easy_687. Longest Univalue Path; 2. Grandyang; 完
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- LeetCode算法题-Longest Univalue Path(Java实现)
这是悦乐书的第290次更新,第308篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第158题(顺位题号是687).给定二叉树,找到路径中每个节点具有相同值的最长路径的长度 ...
- leetcode 687.Longest Univalue Path
寻找最长的路径,那么会在左边或者右边或者是从左到跟然后再到右方的路径的. /** * Definition for a binary tree node. * struct TreeNode { * ...
- [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 ...
- 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 ...
随机推荐
- 仿京东短信验证码UI效果(鸿蒙)
整体思路: 外层Stack布局,里面TextInput组件用来调起键盘,Row布局中循环出四个Text组件,Row布局覆盖在TextInput组件上,用来展示输入的数字. 定义两个参数,code用来接 ...
- Git错误合集 | git工作上遇到的那些报错
前言 我总是在git提交的时候,遇到一些奇奇怪怪的问题.有时候居然还会碰上第二次. 记住这些"绊脚石",下回不摔跤. 目录 git index损坏 一.git index损坏 报错 ...
- CentOS 版本选择:DVD、Everything、LiveCD、Minimal、NetInstall
CentOS 7.X,主要是下载的时候有很多版本供选择,如何选择? DVD版:这个是常用版本,就是普通安装版了,推荐大家安装.里面包含大量的常用软件,大部分情况下安装时无需再在线下载,体积为4G.Ev ...
- delphi+sql数据库增加,删除,修改,查询操作
需要注意的是,open一般用于查询(select),exesql用于修改,插入,删除(update,insert,delete) 增加 with dm.DataModule1.ADOQuery1 do ...
- Towards Accurate Alignment in Real-time 3D Hand-Mesh Reconstruction论文解读
Towards Accurate Alignment in Real-time 3D Hand-Mesh Reconstruction论文解读 这是发表在ICCV2021的一篇文章,主要的工作内容是R ...
- js将 2023-07-13T10:12:23+0800转为 YYYY-MM-DD HH:mm:ss格式
// 封装的日期时间格式化函数 function formatDateTime(dateTimeString) { const inputDate = new Date(dateTimeString) ...
- TimeSpan 的 Milliseconds 和 TotalMilliseconds 有啥区别?
有小伙伴问到 TimeSpan的 Milliseconds和TotalMilliseconds有啥区别啊? 我用TimeSpan.FromSeconds(3600).Milliseconds就获取到是 ...
- Pycharm两种快速激活方式(附最新激活码和插件)
小张的Pycharm最近弹出提示框 Your license has expired提示过期....纳尼!!!! 是不是看到这个也很头疼,.于是我就在想有没有一种方式可以让他永久免费的,于是小张从网上 ...
- CountDownLatch的countDown()方法的底层源码
一.CountDownLatch的构造方法 // 创建倒数闩,设置倒数的总数State的值 CountDownLatch doneSignal = new CountDownLatch(N); 二.c ...
- 基于注解的 AOP 配置
第一步:在 spring 配置文件中开启 spring 对注解 AOP 的支持 <!-- 开启 spring 对注解 AOP 的支持 --> <aop:aspectj-autopro ...