Skip to content

Commit 3fdcbe2

Browse files
authored
Create 0112-path-sum.kt
1 parent f2fe500 commit 3fdcbe2

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

kotlin/0112-path-sum.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Example:
3+
* var ti = TreeNode(5)
4+
* var v = ti.`val`
5+
* Definition for a binary tree node.
6+
* class TreeNode(var `val`: Int) {
7+
* var left: TreeNode? = null
8+
* var right: TreeNode? = null
9+
* }
10+
*/
11+
class Solution {
12+
fun hasPathSum(root: TreeNode?, targetSum: Int): Boolean {
13+
14+
root?: return false
15+
16+
if(root.left == null && root.right == null) return targetSum - root.value == 0
17+
else return hasPathSum(root.left, targetSum - root.value) || hasPathSum(root.right, targetSum - root.value)
18+
}
19+
20+
val TreeNode.value get() = this.`val`
21+
}

0 commit comments

Comments
 (0)