LeetCode #687 Longest Univalue Path 最长同值路径

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

The length of path between two nodes is represented by the number of edges between them.

Example:

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.

题目描述:
给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值。 这条路径可以经过也可以不经过根节点。

注意:两个节点之间的路径长度由它们之间的边数表示。

示例 :

示例 1:

输入:

              5
             / \
            4   5
           / \   \
          1   1   5

输出:

2
示例 2:

输入:

              1
             / \
            4   5
           / \   \
          4   4   5

输出:

2

注意:
给定的二叉树不超过10000个结点。 树的高度不超过1000

思路:

采用递归的思路, 对每一个结点判断, 只要不和当前结点的值相等或者为空就返回长度 0, 否则返回左子树和右子树的长度之和 + 1, 返回所有节点的最长的长度即可
时间复杂度O(n ^ 2), 空间复杂度O(n)

代码:
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) 
    {
        return !root ? 0 : max(get_length(root -> left, root -> val) + get_length(root -> right, root -> val), max(longestUnivaluePath(root -> left), longestUnivaluePath(root -> right)));
    }
private:
    int get_length(TreeNode* root, int val) 
    {
        return (!root or root -> val != val) ? 0 : 1 + max(get_length(root -> left, val), get_length(root -> right, val));
    }
};

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) {
        return root == null ? 0 : Math.max(getLength(root.left, root.val) + getLength(root.right, root.val), Math.max(longestUnivaluePath(root.left), longestUnivaluePath(root.right)));
    }
    
    private int getLength(TreeNode root, int val) {
        return (root == null || root.val != val) ? 0 : 1 + Math.max(getLength(root.left, val), getLength(root.right, val));
    }
}

Python:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def longestUnivaluePath(self, root: TreeNode) -> int:
        def get_length(root: TreeNode, val: int) -> int:
            return 0 if not root or root.val != val else max(get_length(root.left, val), get_length(root.right, val)) + 1 
        return max(get_length(root.left, root.val) + get_length(root.right, root.val), max(self.longestUnivaluePath(root.left), self.longestUnivaluePath(root.right))) if root else 0
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目录 简书的 markdown 都不支持 [TOC] 语法……我就不贴目录了。下面按照类别,列出了29道关于二叉树...
    被称为L的男人阅读 8,627评论 0 8
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,167评论 0 10
  • 又一次没有按时交作业(不开心),每天好多同学都掐着点交作业,为什么要这样呢?每当我们批抨孩子作业都要拖拉到周日晚上...
    ZSZ朱阅读 1,558评论 1 0
  • 喜欢一个人,会纯粹地喜欢他;爱一个人,会完整地爱他。 我喜欢他打篮球的样子,汗滴滑下白嫩的侧脸,我只偷偷看,不尖叫...
    一口甜甜酱阅读 1,180评论 0 0
  • 今天和朋友聊天,他惊叹于我这几年的变化。他说我现在做成的事是他万万想不到的,他想不到一个最讨厌人际关系的人,会做销...
    漠然_然阅读 1,891评论 1 7

友情链接更多精彩内容