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. Openstack的网卡设置

    本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. 最开始接触Openstack,这块是比较头疼的,不同的文档,设置都会有所差异,并 ...

  2. JVM学习--内存分配策略(持续更新)

    一.前言 最近学习<深入java虚拟机>,目前看到内存分配策略这块.本文将进行一些实践. 二.内存分配策略 1.大对象直接进入老年代 书中提到了: 下面进行测试,代码如下: public ...

  3. jenkins不安装任何插件时,是什么样的

    因为jenkins中插件众多,很多时候大家默认安装的话,会默认安装一堆插件.而很多插件会给jenkins的各个方面去增强它, 一致于不清楚哪些功能是插件提供的,还是jenkins自带的. 所以下面将没 ...

  4. 编译安装的gitlab8.x如何修改时区设置

    编译安装的gitlab 8.x版本默认的时区是UTC,在页面上显示的时间默认是零时区的区时,安装完成之后,如果页面上显示的时间比北京时间少了8个小时,则需要修改一下时区 把gitlab.yml文件中的 ...

  5. ruby 升级1.8.7到1.9.3

    rvm install ruby 1.9.3 ruby -v 如果还是1.8.7. rvm use 1.9.3 列出所有版本 rvm list 设置默认的版本 rvm --default use x. ...

  6. jquery的ajax()函数中文传值出现乱码完美解决方案

    1.        jquery的ajax()函数 $.ajax({ type: "POST", dataType: "text", url: ".. ...

  7. Tomcat应用的部署记录

    1.先安装jdk,解压jdk-7u17-linux-x64.tar.gz至/opt目录.配置环境变量,在/etc/profile末加入如下内容. JAVA_HOME=/opt/jdk1..0_17 e ...

  8. 分布式搜索elasticsearch几个概念解析

    原文链接:http://blog.csdn.net/july_2/article/details/24367177 介绍下es的几个概念:cluster     代表一个集群,集群中有多个节点,其中有 ...

  9. Swift - 判断应用是否是第一次启动(或当前版本是否第一次启动)

    1 实现原理 (1)我们会发现许多 App 在一次启动时会显示一个新手引导页(下次启动就不会再显示)   (2)其判断原理就是在 AppDelegate 里的 didFinishLaunchingWi ...

  10. ArcEngine TextElement 定位点的问题

    做ArcGIS Add-In开发,需要在ArcMap中数据视图的左上角加一个上标,如果上标与数据中的地名图层重合,则放在右上角. 上标通过ITextElement,IElement,ITextSymb ...