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. gradle-4.1-all.zip

    1. https://services.gradle.org/distributions/ https://services.gradle.org/distributions/gradle-4.1-a ...

  2. tcp连接出现close_wait状态?可能是代码不够健壮

    一.问题概述 今天遇到个小问题. 我们的程序依赖了大数据那边的服务,大数据那边提供了restful接口供我们调用. 测试反映接口有问题,我在本地重现了. 我这边感觉抓包可能对分析问题有用,就用wire ...

  3. vi 撤销操作

    'u' : 撤销上一个编辑操作 'ctrl + r' : 恢复,即回退前一个命令 'U' : 行撤销,撤销所有在前一个编辑行上的操作

  4. docker搭建gitlab、Redmine

    本地使用windows,setting里面切换至linux 从Docker图标的右键菜单中选中 “Switch to Linux containers ...” Docker Engine运行在Lin ...

  5. Unity3D笔记 英保通九 创建数

    Unity中创建树:可以直接通过程序自动来创建树木还可以手动创建树木(本质上在我看来就是给程序自动创建的树动动”小手术“) 一.程序自动创建树木 3.1.层次视图中创建:一个平行光.摄像机.地.数并且 ...

  6. Postfix邮件

    一() 邮件相关协议 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议, 工作在TCP的25端口.它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的 ...

  7. poj3168 Barn Expansion【计算几何 平面扫描】

    Farmer John has N (1 <= N <= 25,000) rectangular barns on his farm, all with sides parallel to ...

  8. Mapreduce 原理及程序分析

    1.MapReduce(Map+Reduce) 提出一个问题: 目标:你想数出一摞牌中有多少张黑桃. 直观方式:一张一张检查并且数出有多少张是黑桃数目 MapReduce方法则是: 给在座的所有玩家中 ...

  9. CTP API开发之一:CTP API简介

    官网下载CTP API 综合交易平台CTP(Comprehensive Transaction Platform)是由上海期货信息技术有限公司(上海期货交易所的全资子公司)开发的期货交易平台,CTP平 ...

  10. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...