Medium
Given the root
of a binary tree, return 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 the path between two nodes is represented by the number of edges between them.
Example 1:
Input: root = [5,4,5,1,1,null,5]
Output: 2
Explanation: The shown image shows that the longest path of the same value (i.e. 5).
Example 2:
Input: root = [1,4,5,4,4,null,5]
Output: 2
Explanation: The shown image shows that the longest path of the same value (i.e. 4).
Constraints:
[0, 104]
.-1000 <= Node.val <= 1000
1000
.import com_github_leetcode.TreeNode
/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun longestUnivaluePath(root: TreeNode?): Int {
if (root == null) {
return 0
}
val res = IntArray(1)
preorderLongestSinglePathLen(root, res)
return res[0]
}
private fun preorderLongestSinglePathLen(root: TreeNode?, res: IntArray): Int {
if (root == null) {
return -1
}
var left = preorderLongestSinglePathLen(root.left, res)
var right = preorderLongestSinglePathLen(root.right, res)
left = if (root.left == null || root.`val` == root.left!!.`val`) {
left + 1
} else {
0
}
right = if (root.right == null || root.`val` == root.right!!.`val`) {
right + 1
} else {
0
}
val longestPathLenPassingThroughRoot = left + right
res[0] = res[0].coerceAtLeast(longestPathLenPassingThroughRoot)
return left.coerceAtLeast(right)
}
}