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 same value. This path may or may not pass through the root.
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.
分析:
给定一颗二叉树,求最长的路径,其中路径是相同节点间的边数。
递归求解此问题,对于每一个结点,我们要求此时最大的路径数,而最大路径数是由其左右两个孩子结点决定的,那么递归的终止条件就是当为空结点时,路径数为0,当我们知道左右孩子的最大路径数时,需要判断当前和左右孩子结点是否相同,如果相同则当前的结点的最大路径数应该为左孩子路径数+1/右孩子路径数+1取最大值,同时更新结果为当前结果和以当前结点根结点时的左右孩子路径数的和的最大值。
程序:
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) {
int res = ;
if(root == nullptr)
return res;
univaluePath(root, res);
return res;
}
private:
int univaluePath(TreeNode* root, int& res){
if(root == nullptr)
return ;
int l = univaluePath(root->left, res);
int r = univaluePath(root->right, res);
int pl = ;
int pr = ;
if(root->left != nullptr && root->val == root->left->val)
pl = l + ;
if(root->right != nullptr && root->val == root->right->val)
pr = r + ;
res = max(res, pl + pr);
return max(pl, pr);
}
};
Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int longestUnivaluePath(TreeNode root) {
if(root == null)
return res;
univaluePath(root);
return res;
}
private int res = 0;
private int univaluePath(TreeNode root){
if(root == null)
return 0;
int l = univaluePath(root.left);
int r = univaluePath(root.right);
int pl = 0;
int pr = 0;
if(root.left != null && root.val == root.left.val)
pl = l + 1;
if(root.right != null && root.val == root.right.val)
pr = r + 1;
res = Math.max(res, pl + pr);
return Math.max(pl, pr);
} }
LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)的更多相关文章
- [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 ...
- Leetcode687.Longest Univalue Path最长同值路径
给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值. 这条路径可以经过也可以不经过根节点. 注意:两个节点之间的路径长度由它们之间的边数表示. 示例 1: 输入: 5 / \ 4 5 / ...
- [LeetCode] 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
寻找最长的路径,那么会在左边或者右边或者是从左到跟然后再到右方的路径的. /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LeetCode】687. Longest Univalue Path 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- 【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 ...
- 687. Longest Univalue Path
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
随机推荐
- 一个自己实现的Vector 完善版本
一个自己实现的Vector(只能处理基本类型数据) 转载自: https://www.ev0l.art/index.php/archives/22/ string 类型不行 bool char* in ...
- java格式化代码(java格式化代码工具类)
下别人的原来链接..... 支持效果不好要想格式化好需要解析语法树 7个积分我这里免费下 转自 https://download.csdn.net/download/jkl012789/ ...
- P1051复数乘法
P1051复数乘法 转跳点:
- 【pwnable.kr】cmd2
这道题是上一个cmd1的升级版 ssh cmd2@pwnable.kr -p2222 (pw:mommy now I get what PATH environmentis for :)) 登录之后, ...
- Pycharm使用python3无法通过HTMLTestRunner生成测试报告《转载》
Pycharm使用python3无法通过HTMLTestRunner生成测试报告: https://blog.csdn.net/weixin_38981172/article/details/8238 ...
- Spring源码分析——(001)环境搭建
1.官方参考 spring-framework的github链接:https://github.com/spring-projects/spring-framework 源码环境搭建官方参考1:考如何 ...
- Erlang/Elixir精选-第5期(20200106)
The forgotten ideas in computer science-Joe Armestrong 在2020年的第一期里面,一起回顾2018年Joe的 The forgotten idea ...
- 八十四、SAP中的ALV创建之三,创建ALV表格
一.销售表是2个表,一个抬头表,一个是销售内容表,数据库查询语句如下, 二.我们添加相关LAYOUT的格式控制如下 三.需要报每个字段都用相应的LAYOUT控制一下 四.点击模式,在模式里面,添加RE ...
- SQL优化工具 - SQL Server Profiler与数据库引擎优化顾问
最近项目做到几千个学生分别去人脸识别记录(目前约630000行)中查询最后一次记录,可想而知性能这块是个麻烦.于是乎,GET到了SQL Server Profiler和数据库引擎优化顾问这俩工SHEN ...
- 实验吧-web-天下武功唯快不破(Python中byte和str的转换)
题目:看看响应头 打开网站,既然已经提示我们看响应头了,那我们就看看呗(习惯bp,也可直接F12查看) 可以看到,响应头部分有个FLAG,而且有提示:please post what you find ...