forked from neetcode-gh/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0112-path-sum.kt
21 lines (18 loc) · 584 Bytes
/
0112-path-sum.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
classSolution {
funhasPathSum(root:TreeNode?, targetSum:Int): Boolean {
root?:returnfalse
if(root.left ==null&& root.right ==null) return targetSum - root.value ==0
elsereturn hasPathSum(root.left, targetSum - root.value) || hasPathSum(root.right, targetSum - root.value)
}
valTreeNode.value get() =this.`val`
}