File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments