MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / maxPathSum

Function maxPathSum

kotlin/Trees/MaximumPathSum.kt:13–19  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11*/
12
13fun maxPathSum(root: TreeNode?): Int {
14 // In Kotlin, global variable is bad practice.
15 // So, we use a mutable list to store the max sum and access it by reference.
16 var maxSum = mutableListOf(Int.MIN_VALUE)
17 maxPathSumHelper(root, maxSum)
18 return maxSum[0]
19}
20
21fun maxPathSumHelper(node: TreeNode?, maxSum: MutableList<Int>): Int {
22 // Base case: null nodes have no path sum.

Callers

nothing calls this directly

Calls 1

maxPathSumHelperFunction · 0.70

Tested by

no test coverage detected