[抄题]:

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]:

[思维问题]:

  1. 因为可以不从root开始,以为要分情况:进行后续计算之后发现可以合并:从头开始肯定比较长,没必要讨论
  2. 以为左右两边也要分开讨论:结果求和加一下就行了,还是见得太少

[一句话思路]:

点在线段的dfs上加一,没地方直接加,需要单独写公式

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

Example:

                ...
/
4 (res = resl + resr = 3)
(resl = 2) / \ (resr= 1)
(l = 1) 4 4 (r = 0)
/
4

点数是线段数+1

[一刷]:

  1. DFS是嵌套traverse的过程,不能当作返回值,加深理解一下
  2. 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的更多相关文章

  1. [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 ...

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

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

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

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

  4. 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 ...

  5. 【Leetcode_easy】687. Longest Univalue Path

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

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. Leetcode687.Longest Univalue Path最长同值路径

    给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值. 这条路径可以经过也可以不经过根节点. 注意:两个节点之间的路径长度由它们之间的边数表示. 示例 1: 输入: 5 / \ 4 5 / ...

随机推荐

  1. JavaFX 之窗口拖动(三)

    一.问题场景 在上一篇中,我们将窗口的默认标题栏隐藏从而导致鼠标点击窗体无法进行拖动. 二.解决思路 给组件添加鼠标按下事件监听器和鼠标拖动事件监听器. 三.代码实现 /** * 程序入口 * @au ...

  2. 使用Maven运行Solr(翻译)

    Solr是一个使用开源的搜索服务器,它采用Lucene Core的索引和搜索功能构建,它可以用于几乎所有的编程语言实现可扩展的搜索引擎. Solr的虽然有很多优点,建立开发环境是不是其中之一.此博客条 ...

  3. VMware下安装的Mac OS X如何修改显示分辨率 (转)

    我在Win7下利用VMware安装了苹果的OS x 10.8系统,安装成功启动后,发现分辨率为1024*768,而宿机的分辨率是1440*900,我想让虚拟机全屏显示,也就是想在雪豹下屏幕的分辨率也能 ...

  4. Mock&Spring集成

    Mock&Spring集成 常规Mock单元测试 请参考上一篇文档Mock mock框架的功能性对比 http://jmockit.github.io/MockingToolkitCompar ...

  5. elasticSearch Java Spring Data Api

    1. BoolQueryBuilder qb=QueryBuilders. boolQuery(); qb.should(QueryBuilders.matchQuery("keyWord& ...

  6. windows下搭建nginx-rtmp服务器

    windows下搭建nginx-rtmp服务器 windows下搭建nginx-rtmp服务器 准备工作 安装MinGW 安装Mercurial 安装strawberryperl 安装nasm 下载n ...

  7. ThinkPHP 目录结构

    2.0 ThinkPHP 目录结构 在前面的博客中,通过一个简单的案例向大家演示了在ThinkPHP 框架下开发的大致法程,本篇博客将对ThinkPHP框架目录结构进行详细讲解. 要想在项目中熟练地使 ...

  8. 为什么 ReactJS 不适合复杂的前端项目?

    问题一:ReactJS组件难以在复杂交互页面中复用 ReactJS中的最小复用单位是组件.ReactJS的组件比AngularJS的Controller和View 要轻量些. 每个组件只需要前端开发者 ...

  9. LinkedHashMap学习

    一.概述 LinkedHashMap继承自HashMap,是Map接口的一个具体实现,它是有序的,可以按照插入顺序先后和访问时间先后进行排序,选择哪种排序方式取决于在新建LinkedHashMap的时 ...

  10. JSR-303 结合spring 校验

    使用注解 一.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-validator-4.2.0.Final.jar是对上述接口的实现: ...