【LeetCode】687. Longest Univalue Path 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/longest-univalue-path/description/
题目描述
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.
题目大意
求一个二叉树中,相等数值的节点之间的路径最长是多少。
解题方法
DFS
基本思想就是dfs,求一个顶点到所有根节点的路径,时刻保留相等元素的最大值。相等元素的最大值是左右子树的相等元素的最大值+1,所以是递归。
定义的DFS函数是获得在通过root节点的情况下,最长单臂路径。其中更新的res是左右臂都算上的。所以这个题和普通的题是有点不一样。
可以见LeetCode 687. Longest Univalue Path解法。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def longestUnivaluePath(self, root):
"""
:type root: TreeNode
:rtype: int
"""
longest = [0]
def dfs(root):
if not root:
return 0
left_len, right_len = dfs(root.left), dfs(root.right)
left = left_len + 1 if root.left and root.left.val == root.val else 0
right = right_len + 1 if root.right and root.right.val == root.val else 0
longest[0] = max(longest[0], left + right)
return max(left, right)
dfs(root)
return longest[0]
二刷,python写法,思路和上面相同。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def longestUnivaluePath(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root: return 0
self.res = 0
self.getPath(root)
return self.res
def getPath(self, root):
if not root: return 0
left = self.getPath(root.left)
right = self.getPath(root.right)
pl, pr = 0, 0
if root.left and root.left.val == root.val: pl = 1 + left
if root.right and root.right.val == root.val: pr = 1 + right
self.res = max(self.res, pl + pr)
return max(pl, pr)
C++版本的如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int longestUnivaluePath(TreeNode* root) {
if(root == nullptr) return 0;
int res = 0;
getPath(root, res);
return res;
}
private:
int getPath(TreeNode* root, int &res){
if(root == nullptr) return 0;
int l = getPath(root->left, res);
int r = getPath(root->right, res);
int pl = 0, pr = 0;
if(root->left && (root->left->val == root->val)) pl = l + 1;
if(root->right && (root->right->val == root->val)) pr = r + 1;
res = max(res, pl + pr);
return max(pl, pr);
}
};
日期
2018 年 2 月 3 日
2018 年 11 月 24 日 —— 周日开始!一周就过去了~
【LeetCode】687. Longest Univalue Path 解题报告(Python & C++)的更多相关文章
- [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 ...
- 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 ...
- leetcode 687.Longest Univalue Path
寻找最长的路径,那么会在左边或者右边或者是从左到跟然后再到右方的路径的. /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【Leetcode_easy】687. Longest Univalue Path
problem 687. Longest Univalue Path 参考 1. Leetcode_easy_687. Longest Univalue Path; 2. Grandyang; 完
- 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 ...
- [LeetCode] 687. Longest Univalue Path_Easy tag: DFS recursive
Given a binary tree, find the length of the longest path where each node in the path has the same va ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
随机推荐
- (转载)VB中ByVal与ByRef的区别
ByVal是按值传送,在传的过程中不会改变原来的值,仅仅传送的是一个副本, 而 ByRef相反,从内存地址来说,后者是同一个内存地址. ByVal 与 ByRef(默认值)这两个是子过程的参数传递时, ...
- 避免UE4项目VS中误改源码.h文件导致编译时间长
最近几天两次触发VS中误改UE4源码头文件,导致需要编译大量源码的情况:再好的习惯也有不可靠的时候,还是需要可靠方案解决这个问题:官方提供了预编译版本(即从Launcher中下载的版本),但是对于程序 ...
- LeetCode最富有客户的资产总量
最富有客户的资产总量 题目描述 给你一个 m * n 的整数网格 accounts,其中 account[i][j]是第 i 位客户在第 j 家银行托管的资产数量.返回最富有客户所拥有的资产总量. 客 ...
- [PE结构]导入表与IAT表
导入表的结构导入表的结构 typedef struct _IMAGE_IMPORT_DESCRIPTOR { union { DWORD Characteristics; // 0 for termi ...
- Lottie 使用
原文:https://mp.weixin.qq.com/s?__biz=MzIxNjc0ODExMA==&mid=2247485033&idx=1&sn=54dd477b4c4 ...
- 利用unordered_map维护关联数据
在leetcode上刷339题Evaluate Division(https://leetcode.com/problems/evaluate-division/#/description)时在脑中过 ...
- InnoDB的行锁模式及加锁方法
MYSQL:InnoDB的行锁模式及加锁方法 共享锁:允许一个事务度一行,阻止其他事务获取相同数据集的排他锁. SELECT * FROM table_name WHERE ... LOCK IN S ...
- CentOS 6.4 下 Python 2.6 升级到 2.7
一开始有这个需求,是因为用 YaH3C 替代 iNode 进行校园网认证时,CentOS 6.4下一直编译错误,提示找不到 Python 的某个模块,百度了一下,此模块是在 Python2.7 以上才 ...
- BigDecimal 计算注意事项
BigDecimal 在进行除法运算(divide)时一定要注意:如果被除数为变量,一定要指定精度 和 舍入模式,否则会报:Non-terminating decimal expansion; no ...
- Spring是如何保证同一事务获取同一个Connection的?使用Spring的事务同步机制解决:数据库刚插入的记录却查询不到的问题(转)
前言 关于Spring的事务,它是Spring Framework中极其重要的一块.前面用了大量的篇幅从应用层面.原理层面进行了比较全方位的一个讲解.但是因为它过于重要,所以本文继续做补充内容:Spr ...