最长的相同节点值路径 · 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
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
- 因为可以不从root开始,以为要分情况:进行后续计算之后发现可以合并:从头开始肯定比较长,没必要讨论
- 以为左右两边也要分开讨论:结果求和加一下就行了,还是见得太少
[一句话思路]:
点在线段的dfs上加一,没地方直接加,需要单独写公式
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
Example:
...
/
4 (res = resl + resr = 3)
(resl = 2) / \ (resr= 1)
(l = 1) 4 4 (r = 0)
/
4
点数是线段数+1
[一刷]:
- DFS是嵌套traverse的过程,不能当作返回值,加深理解一下
- DFS求的是左边或右边单独的最大值,不是合集,稍微理解下 res[0]是所有值中的最大,需要比较,理解题目
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
DFS求的是单边最大值
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
【重磅】java传递的是引用而不是数值,所以必须是数组才有用
[关键模板化代码]:
三元运算符把DFS特殊情况、扩展直接写了,头次见
int resl = (root.left != null && root.val == root.left.val) ? l + 1 : 0;
int resr = (root.right != null && root.val == root.right.val) ? r + 1 : 0;
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int longestUnivaluePath(TreeNode root) {
//corner case
if (root == null) {
return 0;
}
int[] res = new int[1];
//dfs
dfs(root, res);
//return res[0];
return res[0];
} public int dfs(TreeNode root, int[] res) {
//int max = 0;
int l = (root.left != null) ? dfs(root.left, res) : 0;
int r = (root.right != null) ? dfs(root.right, res) : 0;
//if root == root.left, +1
int resl = (root.left != null && root.val == root.left.val) ? l + 1 : 0;
int resr = (root.right != null && root.val == root.right.val) ? r + 1 : 0;
//res[0] is sum
res[0] = Math.max(res[0], resl + resr);
//return the bigger one of l, r
return Math.max(resl, resr);
}
}
最长的相同节点值路径 · Longest Univalue Path的更多相关文章
- [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 ...
- LeetCode算法题-Longest Univalue Path(Java实现)
这是悦乐书的第290次更新,第308篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第158题(顺位题号是687).给定二叉树,找到路径中每个节点具有相同值的最长路径的长度 ...
- 【LeetCode】687. Longest Univalue Path 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- 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_easy】687. Longest Univalue Path
problem 687. Longest Univalue Path 参考 1. Leetcode_easy_687. Longest Univalue Path; 2. Grandyang; 完
- [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] Longest Univalue Path 最长相同值路径
Given a binary tree, find the length of the longest path where each node in the path has the same va ...
- [Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- Leetcode687.Longest Univalue Path最长同值路径
给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值. 这条路径可以经过也可以不经过根节点. 注意:两个节点之间的路径长度由它们之间的边数表示. 示例 1: 输入: 5 / \ 4 5 / ...
随机推荐
- RK3288 USB UVC camera 摄像头 VIDIOC_DQBUF Failed!!! err[I/O error]
RK3288 Android5.1 多个品牌USB摄像头 同一块主板和代码,大部分品牌的USB摄像头可以正常使用,只有某一款USB摄像头不能使用. 插上摄像头,底层可以识别到摄像头. &l ...
- 给DB2增加删除字段二三事
加字段用这个,CS.TG表名,LINE2_TYPE字段名,CHAR(1)字段类型 ALTER TABLE CS.TG ADD COLUMN LINE2_TYPE CHAR(1); 要是加错了用以下语句 ...
- HTML转义字符表
- 【UVA】11464 Even Parity(枚举子集)
题目 传送门:QWQ 分析 标准的套路题. 枚举第一行,接着根据第一行递推下面的行. 时间复杂度$ O(2^n \times n^2) $ 代码 #include <bits/stdc++.h& ...
- MariaDB主从异步复制详解
一 异步复制(Asynchronous replication) 1.MariaDB本身支持单向的.异步的复制.异步复制意味着在把数据从一台机器拷贝到另一台机器时有一个延时,最重要的是这意味着当应用系 ...
- 如何在OS X 10.9 Mavericks下安装Command Line Tools(命令行工具)
随着OS X 10.9 于 2013年6月10日在旧金山WWDC(world wide developer conference)上发布.是首个不使用猫科动物命名的系统,而转用加利福尼亚的产物. 该系 ...
- 编写一个参数JavaScript函数parseQueryString,它的用途是把url参数解析为一个对象
var url = "http://www.taobao.com/index.php?key0=0&key1=1&key2=2............."; var ...
- 【转】C#父类与子类的静态成员变量、实例成员变量、构造函数的执行顺序
原文地址:http://www.xuebuyuan.com/1092603.html Win7+VS2010测试的结果如下: ①子类静态成员变量②子类静态构造函数③子类实例成员变量④父类静态成员变量⑤ ...
- VMware虚拟机如何设置从U盘启动
要给虚拟机重新安装win7系统,想使用U盘重装系统的方式,就需要让虚拟机从U盘启动,以下内容就是虚拟机从U盘启动的全操作过程. 前期准备: 1.u盘启动盘 2.VMware虚拟机 具体操作步骤: 1. ...
- WampServer之php、mysql环境安装
WampServer之php.mysql环境安装 WampServer介绍: WampServer是一款由法国人开发的Apache Web服务器.PHP解释器以及MySQL数据库的整合软件包.免去了开 ...