专栏首页皮皮瞎学编程Tree - 687. Longest Univalue Path

Tree - 687. Longest Univalue Path

687. Longest Univalue Path

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,因为不允许路径交叉。所以可以看做一个后序遍历求解。

代码:

go:

/**

 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func longestUnivaluePath(root *TreeNode) int {
    if root == nil {
        return 0
    }
    
    var res int
    univaluePath(root, &res)
    
    return res
}

func univaluePath(node *TreeNode, res *int) int {
    if node == nil {
        return 0
    }
    
    leftLen := univaluePath(node.Left, res)
    rightLen := univaluePath(node.Right, res)

    var leftPathLen, rightPathLen int
    if node.Left != nil && node.Val == node.Left.Val {
       leftPathLen = leftLen + 1
    }
    if node.Right != nil && node.Val == node.Right.Val {
      rightPathLen = rightLen + 1
    }
    
    *res = max(*res, leftPathLen + rightPathLen)
    
    return max(leftPathLen, rightPathLen)
    
}

func max(i, j int) int {
    if i > j {
        return i
    }
    
    return j
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Tree - 298. Binary Tree Longest Consecutive Sequence

    298 . Binary Tree Longest Consecutive Sequence

    用户5705150
  • Array - 239. Sliding Window Maximum

    Given an array nums, there is a sliding window of size k which is moving from th...

    用户5705150
  • Tree - 124. Binary Tree Maximum Path Sum

    Given a non-empty binary tree, find the maximum path sum.

    用户5705150
  • 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】

    Big binary tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65...

    Angel_Kitty
  • Tree - 298. Binary Tree Longest Consecutive Sequence

    298 . Binary Tree Longest Consecutive Sequence

    用户5705150
  • 有关vector的存图用法

    用户7727433
  • Code Forces 652C Foe Pairs

    C. Foe Pairs time limit per test 1 second memory limit per test 256 megaby...

    ShenduCC
  • HDUOJ----Eddy's research I

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/...

    Gxjun
  • 算法分析设计--递归算法

    定义: 程序直接或间接调用自身的编程技巧称为递归算法(Recursion)。 一个过程或函数在其定义或说明中又直接或间接调用自身的一种方法,它通常把一个大型...

    风骨散人Chiam
  • Leercode 35 Search Insert Position

    Given a sorted array and a target value, return the index if the target is foun...

    triplebee

扫码关注云+社区

领取腾讯云代金券