-
Notifications
You must be signed in to change notification settings - Fork 0
/
0112_path_sum.swift
47 lines (45 loc) · 1.18 KB
/
0112_path_sum.swift
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// https://leetcode.com/problems/path-sum/
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool {
guard let root = root else { return false }
return check(node: root, target: targetSum)
}
func check(node: TreeNode, target: Int) -> Bool {
let newTarget = target - node.val
var hasChild = false
if let left = node.left {
hasChild = true
let leftResult = check(node: left, target: newTarget)
if leftResult {
return true
}
}
if let right = node.right {
hasChild = true
let rightResult = check(node: right, target: newTarget)
if rightResult {
return true
}
}
if hasChild {
return false
} else {
return newTarget == 0
}
}
}