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. Sublime PlantUML环境配置

    参考[http://www.jianshu.com/p/e92a52770832]在安装中遇到不少问题,总结一次成功的步骤如下 一.安装步骤: 1)准备java  环境 jdk1.7 2)安装Subl ...

  2. 【OOP】C++ const成员函数

    预备知识 1.代码转换分析技巧 在早期某些编译器会将C++代码翻译为C代码,然后使用C编译器生成可执行文件.其中翻译的一个转化就是:将this指针显式添加到成员函数的第一个参数位置上,并在成员函数调用 ...

  3. 【BZOJ4919】[Lydsy六月月赛]大根堆 线段树合并

    [BZOJ4919][Lydsy六月月赛]大根堆 Description 给定一棵n个节点的有根树,编号依次为1到n,其中1号点为根节点.每个点有一个权值v_i. 你需要将这棵树转化成一个大根堆.确切 ...

  4. iOS - 沙盒机制(SandBox)和获取沙盒路径

    iOSAPP可以在自己的沙盒里读写文件,但是,不可以访问其他APP的沙盒.每一个APP都是一个信息孤岛,相互是不可以进行通信的,唯独可以通过URLScheme.沙盒里面的文件可以是照片.声音文件.文本 ...

  5. springmvc如何访问到静态的文件,如jpg,js,css

    如何你的DispatcherServlet拦截"*.do"这样的有后缀的URL,就不存在访问不到静态资源的问题. 如果你的DispatcherServlet拦截"/&qu ...

  6. Web开发中需要了解的东西【转载】

    在StackExchange上有人问了这样一个问题:What should every programmer know about web development?(关于Web开发,什么是所有程序员需 ...

  7. Rsync数据同步应用指南

    1.软件简介 Rsync 是一个本地或远程数据同步工具,基于RSync算法,这个算法是澳大利亚人Andrew Tridgell发明的:可通过 LAN/WAN 快速同步多台主机间的文件.Rsync 本来 ...

  8. Saltstack之SaltSyndic

    SaltSyndic 1,SaltSyndic必须运行在salt-master上面 2,Syndic要连接另外一个Master比它更高级 在master安装syndic yum -y install ...

  9. hihocoder 1330 - 数组重排 - [hiho一下167周][最小公倍数]

    题目链接:https://hihocoder.com/problemset/problem/1330 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi想知道,如果他 ...

  10. 怎么点击div之外的区域就隐藏这个div啊 找了很久,都没有很好解决

    方法一. <!DOCTYPE html><html><head><meta http-equiv="Content-Type" conte ...