-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path112.PathSum.kt
32 lines (29 loc) · 1.04 KB
/
112.PathSum.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* 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
* }
*/
//https://leetcode.com/problems/path-sum/description
class Solution {
fun hasPathSum(root: TreeNode?, targetSum: Int): Boolean {
return dfs(root, 0, targetSum)
}
fun dfs(node: TreeNode?, sum: Int, targetSum: Int): Boolean {
if (node == null) return false
val currentSum = sum + node.`val`
if (node.left == null && node.right == null) {
return currentSum == targetSum
}
return dfs(node.left, currentSum, targetSum) || dfs(node.right, currentSum, targetSum)
}
fun hasPathSum1(root: TreeNode?, targetSum: Int): Boolean {
if (root == null) return false
if (root.left == null && root.right == null && root.`val` == targetSum) return true
return hasPathSum(root.left, targetSum - root.`val`) || hasPathSum(root.right, targetSum - root.`val`)
}
}