Swift recursive solution


  • 0
    S
    /**
     * 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
     *     }
     * }
     * edge cases: root == sum, root == nil
     */
    class Solution {
        func hasPathSum(_ root: TreeNode?, _ sum: Int) -> Bool {
            guard let root = root else { return false }
            if root.val == sum && (root.left == nil && root.right == nil) {
                return true
            }
            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.