LeetCode-687. Longest Univalue Path

标签: LeetCode Tree
0人阅读 评论(0) 收藏 举报
分类:

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

1.The length of path between two nodes is represented by the number of edges between them.
2.The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.

Example 1

Input:

              5
             / \
            4   5
           / \   \
          1   1   5
Output:

2

Example 2

Input:

              1
             / \
            4   5
           / \   \
          4   4   5
Output:

2

Solution 1(C++)

class Solution {
public:
    int longestUnivaluePath(TreeNode* root) {
        if (!root) return 0;
        int longestPath=0;
        helper(root, longestPath);
        return longestPath;
    }
private:
    int helper(TreeNode* root, int& m){
        int l=root->left ? helper(root->left, m) : 0;
        int r=root->right ? helper(root->right, m) : 0;
        l=(root->left && root->left->val==root->val) ? l+1 : 0;
        r=(root->right && root->right->val==root->val) ? r+1 : 0;
        m=max(m,l+r);
        return max(l,r);
    }
};

算法分析

这道题我认为可以看成是最长连续序列与树的结合。利用动态规划的方法可以解决。观察每一个节点。每一个节点所在可能路径最长相同连续序列的长度其实与其左子节点与右子节点有关,考虑到连续序列的特性,一旦不连续了,即根节点的值与左子节点或者与右子节点的值不相等了,那么左边的或者右边的就为0,如果相等,那么左边的或者右边的就各自加1。

这里要注意,关于长度的定义,两个节点长度为1,那么可以认为长度初始值为0,那么每新加一个节点,长度加1。

还是有些地方说不太清楚,给这个题目打了个心,以后在做一做。

程序分析

略。

查看评论

[leetcode] 687. Longest Univalue Path

[leetcode] 687. Longest Univalue Path
  • yaozhili_
  • yaozhili_
  • 2017-11-26 12:43:10
  • 277

leetcode 687 Longest Univalue Path

Java:class Solution { public int longestUnivaluePath(TreeNode root) { int[] res=new int[...
  • MRxjh
  • MRxjh
  • 2018-01-02 16:15:15
  • 84

leetcode 687. Longest Univalue Path

687. Longest Univalue Path Given a binary tree, find the length of the longest path where each ...
  • liqinzhe11
  • liqinzhe11
  • 2017-12-04 10:01:16
  • 143

LWC 52:687. Longest Univalue Path

LWC 52:687. Longest Univalue Path传送门:687. Longest Univalue PathProblem: Given a binary tree, find ...
  • u014688145
  • u014688145
  • 2017-10-02 15:38:06
  • 1530

Longest Univalue Path问题及解法

687. Longest Univalue Path LeetCode
  • u011809767
  • u011809767
  • 2017-10-13 15:20:12
  • 860

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...
  • wjheha
  • wjheha
  • 2017-11-24 09:56:12
  • 80

(树)687. Longest Univalue Path

最近在做树结构的相关习题,因为之前随机抽题的时候发现这个模块总是出错,考虑的不周全,所以想集中做一下。 Given a binary tree, find the length of the l...
  • qq_24153697
  • qq_24153697
  • 2017-11-10 15:18:46
  • 44

【LeetCode】687. Longest Univalue Path 解题报告

【LeetCode】687. Longest Univalue Path 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2018-02-03 19:26:18
  • 29

Leetcode算法学习日志-687 Longest Univalue Path

Leetcode 687 Longest Univalue Path 题目原文 Given a binary tree, find the length of the longest path...
  • Zarlove
  • Zarlove
  • 2018-01-14 10:11:12
  • 165

[LeetCode - tree]687. Longest Univalue Path

687. Longest Univalue PathGiven a binary tree, find the length of the longest path where each node i...
  • dazhangyu97
  • dazhangyu97
  • 2017-12-02 11:52:51
  • 45
    个人资料
    持之以恒
    等级:
    访问量: 1万+
    积分: 2507
    排名: 1万+
    文章存档
    最新评论