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.

这个题目的思路实际上跟 [LeetCode] 124. Binary Tree Maximum Path Sum 很像, 就是用一个helper function(input: root, output: 包括root的最长的univalue path 的number of nodes),

然后注意self.ans 是跟 1+ l + r 比较, 但是返回的时候只返回 1+ max(l, r), 最后返回self.ans -1 (因为题目求的是边)

1. Constraints

1) empty => 0

2) 1 node => 0

3) element will be interger

2. Ideas

DFS      T: O(n)     S; O(n)

1) self.ans = 1 # 这样方便判断如果not root 的情况

2) helper function, check l, r , if not 0, check root.val == root.child.val, update self.ans = max(self.ans, 1+ l+ r)

3) return self.ans -1

3. Code

class Solution:
def maxUnivaluePath(self, root):
ans = [None]
def maxPathWithRoot(root):
if not root: return 0
left, right = maxPathWithRoot(root.left), maxPathWithRoot(root.right)
left = 1 + left if root.left and root.val == root.left.val else 0
right = 1 + right if root.right and root.val == root.right.val else 0
local = max(0, left, right)
potential = max(local, left + right)
if ans[0] is None or potential > ans[0]:
ans[0] = potential
return local
if not root: return 0
maxPathWithRoot(root)
return ans[0]

[LeetCode] 687. Longest Univalue Path_Easy tag: DFS recursive的更多相关文章

  1. [LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

  4. leetcode 687.Longest Univalue Path

    寻找最长的路径,那么会在左边或者右边或者是从左到跟然后再到右方的路径的. /** * Definition for a binary tree node. * struct TreeNode { * ...

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

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

  6. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  7. 【Leetcode_easy】687. Longest Univalue Path

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

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

  9. [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

随机推荐

  1. 三.jquery.datatables.js表格编辑与删除

    1.为了使用如图效果(即将按钮放入行内http://www.datatables.net/examples/ajax/null_data_source.html) 采用了另一个数据格式 2.后台php ...

  2. scanf printf gets() puts(),cin cout

    最近在练机试题,常用的C和C++输入输出如下: 1 scanf 和printf int a; scanf("%d",&a) ; printf("%d", ...

  3. smarty直接在模板中格式化时间的方法

    smarty提供了一个获取时间戳的方法: <%$smarty.now%> 使用该方法获取到当时的时间戳之后,使用格式化修饰符data-format进行修饰: <%$smarty.no ...

  4. vue项目打包之后页面空白解决办法

    之前项目遇到个情况,npm run build打包之后上传到服务器后,index.html打开一片空白,资源都加载了,但是就是不显示. 然后百度找了原因,修改了两处地方 一.修改 assetsPubl ...

  5. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十六:IIC储存模块

    IIC储存器是笔者用来练习精密控时的经典例子.<整合篇>之际,IIC储存器的解释,笔者也自认变态.如今笔者回头望去,笔者也不知道自己当初到底发什么神经,既然将IIC的时序都解释一番.由于开 ...

  6. 传真AT指令部分(参考)

    不知道下面的命令是不是通用的,如果有尝试过的师兄给我个回复!! 列出了您的MODEM能理解的传真 AT 命令.每个命令描述包括命令名称.解释和相关参数. 传真命令 命令 描述 +F<comman ...

  7. AutoMapper queryable extensions 只找需要的字段

    http://jahav.com/blog/automapper-queryable-extensions/ How to generate a LINQ query for your DTOs Au ...

  8. Java Agent初探——动态修改代码

    用了一下午总算把java agent给跑通了,本篇文章记录一下具体的操作步骤,以免遗忘... 通过java agent可以动态修改代码(替换.修改类的定义),进行AOP. 目标: ? 1 为所有添加@ ...

  9. 'cmd' 不是内部或外部命令,也不是可运行的程序 或批处理文件.

    添加path内容: %SystemRoot%\system32; %SystemRoot%; %SystemRoot%\System32\Wbem;

  10. Thread和Runable的区别、Synchronized锁关键字

    一.Thread和Runable的区别 Thread是基类,子类必继承他实现其run方法.其也是实现了Runable接口.Thread是普通的类,并非抽象类或者密封类等. Runnable是接口,子类 ...