题目:

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)的更多相关文章

  1. [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 ...

  2. Leetcode687.Longest Univalue Path最长同值路径

    给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值. 这条路径可以经过也可以不经过根节点. 注意:两个节点之间的路径长度由它们之间的边数表示. 示例 1: 输入: 5 / \ 4 5 / ...

  3. [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 ...

  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_easy】687. Longest Univalue Path

    problem 687. Longest Univalue Path 参考 1. Leetcode_easy_687. Longest Univalue Path; 2. Grandyang; 完

  7. 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 ...

  8. [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 ...

  9. 687. Longest Univalue Path

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

随机推荐

  1. leetcode1305 All Elements in Two Binary Search Trees

    """ Given two binary search trees root1 and root2. Return a list containing all the i ...

  2. ListView的DrawSubItem时间添加边框,字体变粗问题

    procedure TFrmrdp.ListView1AdvancedCustomDrawSubItem(Sender: TCustomListView; Item: TListItem; SubIt ...

  3. 吴裕雄--天生自然java开发常用类库学习笔记:线程的生命周期

    class MyThread implements Runnable{ private boolean flag = true ; // 定义标志位 public void run(){ int i ...

  4. JuJu团队11月29号工作汇报

    JuJu团队11月29号工作汇报 JuJu   Scrum 团队成员 今日工作 剩余任务 困难 于达  生成所有mini batch, 支持不同batch_size  优化代码  熟悉julia 婷婷 ...

  5. Golang的基础数据类型-浮点型

    Golang的基础数据类型-浮点型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.浮点型概述 Go语言提供两种精度的浮点数,即float32和float64,其中float32 ...

  6. bzoj 3696: 化合物

    哦,这个困惑了我好久的东西——生成函数(母函数),(然而拿这个东西去向学文化课的同学装逼并不成功...) 生成函数,就是把原来的加法组合变成乘法的指数加法,那么我们要求的值就是相应的指数的系数的值啦, ...

  7. 球队“食物链”(DFS+剪枝)

    某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席突发奇想,希望从中找出一条包含所有球队的“食物链 ...

  8. 这篇干货让你在零点前完成学术Essay写作

    写论文,做研究,上课,参加课外活动,与他人social...在美国,你会有很多的事情需要你去做,如何将自己的时间平衡的分配到自己的学习生活以及私人生活中,就显得尤为重要,而这些问题也是影响中国学生的重 ...

  9. Spark 调优

    资源调优 (1). 在部署 spark 集群中指定资源分配的默认参数 在 spark 安装包的 conf 下的 spark-env.sh SPARK_WORKER_CORES SPARK_WORKER ...

  10. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring JDBCTemplate简介

    Spring 框架针对数据库开发中的应用提供了 JDBCTemplate 类,该类是 Spring 对 JDBC 支持的核心,它提供了所有对数据库操作功能的支持. Spring 框架提供的JDBC支持 ...