[抄题]:

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. BZOJ3261:最大异或和

    浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:https://lydsy.com/JudgeOnline/problem ...

  2. php项目有负载,$_SERVER['HTTP_X_FORWARDED_FOR']函数在不同系统中获取到的值形式不一样,ios系统苹果手机只能获取到一个ip(113.87.214.xxx),而安卓手机获取到的是2个ip中间逗号隔开的形式(113.87.214.xxx , xxx.xxx.xxx.xxx)

    这次由于有个抽奖活动功能,苹果手机每次都抽奖失败,安卓手机每次都抽奖失败(5台ios手机,8台Android手机). 错误日志查看是因为,抽奖用户的ip记录进数据库时出错,之前都是拿到ip直接插入数据 ...

  3. 使用Apache POI操作Excel文件---在已有的Excel文件中插入一行新的数据

    package org.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...

  4. Nginx (一)Windows下编译Nginx源码以及安装 nginx for windows方法步骤

    转载自: http://apps.hi.baidu.com/share/detail/11192699#content Nginx介绍: Nginx ("engine x")是一个 ...

  5. java http头信息

    JAVA 从http请求头中获取Header信息: request.getHeader(),request.getHeaderNames(),request.getHeaders() Java获取Ht ...

  6. Redis: temple

    ylbtech-Redis:  1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   9.返回顶部   1 ...

  7. ThinkPHP Http工具类(用于远程采集 远程下载) phpSimpleHtmlDom采集类库_Jquery筛选方式 使用phpQuery轻松采集网页内容http://www.thinkphp.cn/extend/541.html

    [php]代码库 view sourceprint? <?php // +------------------------------------------------------------ ...

  8. python复习之路-Day01

    数据类型初识 1.数字 2 是一个整数的例子.长整数 不过是大一些的整数.3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4.(-5+4j)和 ...

  9. ThinkPHP 配置详解

      3.0 ThinkPHP配置详解 3.1 入口文件的配置 一般不建议在入口文件做过多的配置,但可以重新定义一些系统常量,以下简单介绍几个常用的系统常量. 1.APP_PATH 默认情况下,框架的项 ...

  10. Nginx记录客户端POST过来的具体信息

    vim nginx/config/nginx.config $request_body这个变量值就是POST数据 log_format main '$remote_addr - $remote_use ...