leetcode 687题

问题: 

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

解答:

# 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):
        """
        :type root: TreeNode
        :rtype: int
        """
        
        """
            5(a)
            /
           5(b)
         /    \
        5(c)   5(d)
        
        经过某一节点的最长的path,可能有三种情况
        a-b-c
        a-b-d
        c-b-d
        需要计算三种情况的最大值,
        记录 a-b 的长度
        记录 b-c 的长度
        记录 b-d 的长度
        
        """
        def path(root,val_dict):
            if root.val not in val_dict:
                val_dict.update({root.val:0})
                
            if root.left==None and root.right==None:
                return 0
            # 左侧
            left_val = 0
            if root.left!=None:
                if root.left.val == root.val:
                    left_val = path(root.left,val_dict)+1
                else:
                    path(root.left,val_dict)
                    
            # 右侧     
            right_val = 0
            if root.right!=None:
                if root.right.val == root.val:
                    right_val = path(root.right,val_dict)+1
                else:
                    path(root.right,val_dict)
            
            # 更新dict
            # 处理了多种情况,c-b-d形式
            # 1) right_val==0,left_val!=0
            # 2) right_val!=0,left_val==0
            # 3) right_val!=0,left_val!=0
            if root.val in val_dict  and val_dict[root.val]< right_val+left_val:
                val_dict[root.val] = right_val+left_val
            
            # 父亲节点需要这个数字 a-b-d 或者 a-b-c 形式
            return max(left_val,right_val)
        
        val_dict = {}
        if root!=None:
            path(root,val_dict)
            return max(val_dict.values())
        return 0

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LeetCode是一个著名的在线编程库,可以帮助计算机程序员提升编程能力。LeetCode目涵盖了算法、数据结构、字符串、数组、链表等多个方面的知识,对于求职面试以及算法竞赛准备非常有帮助。 LeetCode上的编程目分为简单、中等和困难三个难度级别。每个目都有详细的目描述、输入输出示例以及参考答案等内容。在解决每个问时,我们需要仔细阅读目,并根据目要求提供正确的解答。 通常,我们需要使用编程语言如Python、C++等来实现解思路。在编码之前,我们需要先分析问,并找到合适的算法或数据结构来解决问。一般来说,我们可以使用递归、迭代、动态规划等方法来解决常见的编程问。 在LeetCode上,我们可以提交解答,并测试解答是否通过了所有的测试用例。如果通过了所有的测试用例,我们就可以得到目的AC(Accepted)结果,并获得该目的通过证书。如果没有通过所有的测试用例,我们可以检查自己的解答,查找解答中的错误或者不完善之处,并进行修改和优化。 总之,LeetCode编程是一个优秀的学习和练习编程的平台。通过解答LeetCode上的编程目,我们可以提升自己的编程能力,并且培养解决问的思维方式。无论是求职面试还是算法竞赛,LeetCode编程都是非常有帮助的资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值