[go: nahoru, domu]

Skip to content

Commit

Permalink
Submit 112_PathSum
Browse files Browse the repository at this point in the history
  • Loading branch information
Binlogo committed Jul 8, 2020
1 parent 007d14f commit f63f4de
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//: [Previous](@previous)

import Foundation


/// Definition for a binary tree node.
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init(_ val: Int) {
self.val = val
self.left = nil
self.right = nil
}
}

class Solution {
func hasPathSum(_ root: TreeNode?, _ sum: Int) -> Bool {
guard let root = root else {
return false
}
if root.left == nil && root.right == nil {
return root.val == sum
}
return hasPathSum(root.left, sum - root.val)
|| hasPathSum(root.right, sum - root.val)
}
}

//: [Next](@next)

0 comments on commit f63f4de

Please sign in to comment.