Skip to content

Commit 1e43c09

Browse files
author
smishralm10
committed
solved problem 0112-path-sum in swift
1 parent e2aab2b commit 1e43c09

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

swift/0112-path-sum.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool {
3+
guard let root = root else { return false }
4+
5+
if root.val == targetSum && !hasChild(root) { return true }
6+
7+
return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val)
8+
}
9+
10+
func hasChild(_ root: TreeNode) -> Bool {
11+
return (root.left != nil) || (root.right != nil)
12+
}
13+
}

0 commit comments

Comments
 (0)