[抄题]:

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. oracle之 调整 I/O 相关的等待

    I/O相关竞争等待简介 当Oracle数据库出现I/O相关的竞争等待的时候,一般来说都会引起Oracle数据库的性能低下,发现数据库存在I/O相关的竞争等待一般可以通过以下的三种方法来查看Oracle ...

  2. cookie控制登陆时间

    使用cookie实现永久登陆 1,在cookie里面保存账号密码然后和数据库核对(由于我没有使用数据库,就不用了 2,在cookie里面保存时间戳和账号使用加密解密(我也没有使用时间戳 思路,requ ...

  3. CentOS yum源设定使用方法的整理(转)

    CentOS yum更新了很多版本更新,我本人认为CentOS yum很好使的文件系统,在此向大家推荐CentOS应该是做为服务器的linux的佼佼者.CentOS采用的二进制包是rpm,不过包的依赖 ...

  4. (转)Android 读取联系人(详细)

    import java.io.InputStream; import org.json.JSONArray; import org.json.JSONException; import org.jso ...

  5. 皆在FPGA之外

    最近做电力方面的项目,由于跨行业,所以很长一段时间都在做前期准备工作. 项目设计前应尽量做到面面俱到,否则会在项目设计中遇到下面大概率问题: 性能不满足需求,然后为了提升性能,资源又成了瓶颈: 功能设 ...

  6. [转]自己做 Visual Studio 2013 代码折叠插件

    本文代码来自:https://msdn.microsoft.com/zh-cn/library/ee197665.aspx 第一步.引用 -> 程序集 -> 扩展 里找到对应 .dll 添 ...

  7. 竖屏拍照,但是sd卡中却是横屏解决方法

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) ...

  8. bytes数据类型和字符串的编码解码,三元运算,进制互换

    三元运算 如果这个条件成立就存这个值,如果那个条件成立就存那个值. 进制 bytes类型,字节数据类型也就是二进制类型,这个是python3专有数据类型,在python2里跟字符串是一个类型,也就是p ...

  9. thinkphp中的配置与读取C方法详解

    1.项目公共配置 Conf/config.php 内容如下 <?php /** *项目公共配置 *@package *@author **/ return array( 'LOAD_EXT_CO ...

  10. springmvc 打包遇到的问题

    1.测试有错误,过不去,mvn install -DSkipTests 2.设置resources路径,在pom.xml中添加 <build> <resources> < ...