Swift solution


  • 0
    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)
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.