美文网首页
LeetCode - Sum of Root To Leaf B

LeetCode - Sum of Root To Leaf B

作者: Andy1944 | 来源:发表于2019-07-19 14:56 被阅读0次

Sum of Root To Leaf Binary Numbers - LeetCode

Solution

class Solution {
    func sumRootToLeaf(_ root: TreeNode?) -> Int {
        return getLeafString(root, value: []).map { (num) in
            return binTodec(number: num)
        }.reduce(0, +)
    }
    
    func getLeafString(_ root: TreeNode?, value: [Int]) -> [String] {
        guard let root = root else {
            return []
        }
        if root.left == nil && root.right == nil {
            return [(value + [root.val]).map{ String($0) }.joined()]
        } else {
            return getLeafString(root.left, value: value + [root.val]) + getLeafString(root.right, value: value + [root.val])
        }
    }
    
    func binTodec(number num: String) -> Int {
        var sum: Int = 0
        for c in num {
            let str = String(c)
            sum = sum * 2 + Int(str)!
        }
        return sum
    }
}

解题思路

递归取出叶子节点的值,转换成十进制相加

相关文章

网友评论

      本文标题:LeetCode - Sum of Root To Leaf B

      本文链接:https://www.haomeiwen.com/subject/btfslctx.html