| 11 | */ |
| 12 | |
| 13 | fun 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 | |
| 21 | fun maxPathSumHelper(node: TreeNode?, maxSum: MutableList<Int>): Int { |
| 22 | // Base case: null nodes have no path sum. |
nothing calls this directly
no test coverage detected