<commit_before>//
//  112_PathSum.swift
//  HRSwift
//
//  Created by <NAME> on 2016-08-06.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

/**
	Title:112 Path Sum
	URL: https://leetcode.com/problems/path-sum/
	Space: O(n)
	Time: O(n)
 */

class PathSum_Solution {
  
  var target: Int = Int.max
  
  func hasPathSum(root: TreeNode?, _ sum: Int) -> Bool {
    self.target = sum
    return dfsSum(root, prevSum: 0)
  }
  
  private func dfsSum(root: TreeNode?, prevSum: Int) -> Bool {
    guard let root = root else {
      return false
    }
    let currentSum = root.val + prevSum
    if let left = root.left {
      let leftSatisfied = dfsSum(left, prevSum: currentSum)
      if leftSatisfied {
        return true
      }
    }
    if let right = root.right {
      let rightSatisfied = dfsSum(right, prevSum: currentSum)
      if rightSatisfied {
        return true
      }
    }
    return (root.left == nil) && (root.right == nil) && currentSum == target
  }
}<commit_msg>Improve 112 pathSum<commit_after>//
//  112_PathSum.swift
//  HRSwift
//
//  Created by <NAME> on 2016-08-06.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

/**
	Title:112 Path Sum
	URL: https://leetcode.com/problems/path-sum/
	Space: O(n)
	Time: O(n)
 */

class PathSum_Solution {
  
  var target: Int = Int.max
  
  func hasPathSum(root: TreeNode?, _ sum: Int) -> Bool {
    self.target = sum
    return dfsSum(root, prevSum: 0)
  }
  
  private func dfsSum(root: TreeNode?, prevSum: Int) -> Bool {
    guard let root = root else {
      return false
    }
    
    let currentSum = prevSum + root.val
    
    if (root.left == nil) && (root.right == nil) && currentSum == target {
      return true
    }

    return dfsSum(root.left, prevSum: currentSum) || dfsSum(root.right, prevSum: currentSum)
  }
}
{'fraction_non_alphanumeric': 0.130647795318454, 'fraction_numerical': 0.02231899836690256, 'mean_word_length': 2.41635687732342, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4540124', 'n_tokens_mistral': 694, 'n_tokens_neox': 645, 'n_words': 175}