mathonsunday
2/13/2019 - 12:34 PM

@discardableResult yay or nay

var count = 0
func findSingleValueTrees(root: TreeNode?) -> Int {
    // if root is nil return 0
    guard let root = root else {
        return 0
    }
    findSingleValueTreesHelper(root)
    return count
}

//findSingleValueTreesHelper
@discardableResult
// input: current: TreeNode?, output: Bool
func findSingleValueTreesHelper(_ current: TreeNode?) -> Bool {
    // guard that current is not nil, else return true
    guard let current = current else {
        return true
    }
    let left = findSingleValueTreesHelper(current.left_ptr)
    let right = findSingleValueTreesHelper(current.right_ptr)
    if !left || !right {
        return false
    }
    if let leftNode = current.left_ptr, leftNode.val != current.val {
        return false
    }
    if let rightNode = current.right_ptr, rightNode.val != current.val {
        return false
    }
    
    count += 1
    return true
}