题目:

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. 关于重定向RedirectAttributes的用法

    刚才做项目,遇到了redirectAttributes使用的问题,上网找了找,看到一篇写的很不错的博客,解决我对于RedirectAttributes的困惑,也给大家推荐下. 原文链接:href=&q ...

  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-download

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  3. 单调栈应用--将一个数删除n各数字之后的最大\最小值

    E. Playing with numbers time limit per test 2.0 s memory limit per test 64 MB input standard input o ...

  4. mybatis插入数据后将其ID返回

    背景 mybatis没有关联保存的功能,所以主从表需要分开保存,这就涉及到主表保存后要再次获取主表ID的环节,以下介绍mybatis插入数据后返回其自增ID的两种方式 方案 1.sql获取 <i ...

  5. ssh服务启动失败 /var/empty must be owned by root and not group or world-writable.

    输入 /etc/rc.d/init.d/sshd start 启动sshd服务,报如下错误: /var/empty must be owned by root and not group or wor ...

  6. postman带上token对接口进行测试

    根据验证码进行登陆,登录之后返回token.然后请求其他接口时在header头中带上token访问其他接口进行测试 账户信息输入验证码 登陆成功,看到返回的token 我们这个项目可以刷新一下toke ...

  7. ORIGIN(起源属性)路由起源骗术

    ORIGIN(起源属性)配置: ①:抓取感兴趣流量——prefix.access ②:创建route-map 流量地图——permit 10 ③:匹配感兴趣流量——match ④:设置起源属性——se ...

  8. 九、JavaScript之分号使用,支持一行多语句

    一.代码如下 二.执行效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...

  9. Codeforces 460C 二分结果+线段树维护

    发现最近碰到好多次二分结果的题目,上次多校也是,被我很机智的快速过了,这个思想确实非常不错.在正面求比较难处理的时候,二分结果再判断是否有效往往柳暗花明. 这个题目给定n个数字的序列,可以操作m次,每 ...

  10. java菜鸡循环练习

    While 循环练习  输入3次密码,则打印密码锁定 package com.lv.test; import java.util.Scanner; public class DemoPass { pu ...