Skip to content

Commit f9e31d2

Browse files
committed
437/kt-nopass
1 parent e17e9fe commit f9e31d2

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

437.path-sum-iii.0.[nopass].kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @lc app=leetcode id=437 lang=kotlin
3+
*
4+
* [437] Path Sum III
5+
*/
6+
/**
7+
* Example:
8+
* var ti = TreeNode(5)
9+
* var v = ti.`val`
10+
* Definition for a binary tree node.
11+
* class TreeNode(var `val`: Int) {
12+
* var left: TreeNode? = null
13+
* var right: TreeNode? = null
14+
* }
15+
*/
16+
// TLE
17+
class Solution_pathSum_0 {
18+
fun getPaths(root: TreeNode?): Pair<List<List<Int>>, List<List<Int>>> {
19+
if (root == null) return Pair(listOf<List<Int>>(), listOf<List<Int>>())
20+
21+
val self = listOf(listOf(root.`val`))
22+
23+
val (leftMut, leftInmmut) = getPaths(root.left)
24+
25+
val (rightMut, rightInmmut) = getPaths(root.right)
26+
27+
val nextMut = (leftMut + rightMut)
28+
.filter { it.size > 0 }
29+
.map {
30+
listOf(root.`val`) + it
31+
} + self
32+
33+
val nextImmut = nextMut + leftInmmut + rightInmmut
34+
return Pair(nextMut, nextImmut)
35+
}
36+
37+
fun pathSum(root: TreeNode?, sum: Int): Int {
38+
return getPaths(root).second.filter { it.sum() == sum }.size
39+
}
40+
}

0 commit comments

Comments
 (0)