Skip to content

Commit

Permalink
문제 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalma committed Oct 25, 2024
1 parent cfbe1ce commit 3dc9850
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions binary-tree-maximum-path-sum/jdalma.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package leetcode_study

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import kotlin.math.max

class `binary-tree-maximum-path-sum` {

/**
* TC: O(n), SC: O(log n)
*/
fun maxPathSum(root: TreeNode?): Int {
if (root == null) return 0
var max = root.`val` // 부모 노드와 2개의 자식 노드의 합을 전역 변수로 갱신한다.

fun dfs(node: TreeNode?): Int {
if (node == null) return 0

val left = max(dfs(node.left), 0)
val right = max(dfs(node.right), 0)

max = max(node.`val` + left + right, max)
return node.`val` + max(left, right) // 현재 노드와 2개의 자식 노드 중 최대의 값을 반환한다.
}

dfs(root)
return max
}

@Test
fun `이진 트리의 최대 경로 합을 반환한다`() {
maxPathSum(TreeNode.of(-10,9,20,null,null,15,7)) shouldBe 42
maxPathSum(TreeNode.of(1,9,20,null,null,15,7)) shouldBe 45
}
}

0 comments on commit 3dc9850

Please sign in to comment.