美文网首页
算法练习--LeetCode--129. Sum Root to

算法练习--LeetCode--129. Sum Root to

作者: Crazy凡 | 来源:发表于2019-04-13 18:47 被阅读0次

    129. Sum Root to Leaf Numbers

    Medium
    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
    An example is the root-to-leaf path 1->2->3 which represents the number 123.
    Find the total sum of all root-to-leaf numbers.
    Note: A leaf is a node with no children.

    Example:

    Input: [1,2,3]
       1
      / \
     2   3
    Output: 25
    

    Explanation:
    The root-to-leaf path 1->2 represents the number 12.
    The root-to-leaf path 1->3 represents the number 13.
    Therefore, sum = 12 + 13 = 25.

    Example 2:

    Input: [4,9,0,5,1]
       4
      / \
     9   0
    / \
    5   1
    Output: 1026
    

    Explanation:
    The root-to-leaf path 4->9->5 represents the number 495.
    The root-to-leaf path 4->9->1 represents the number 491.
    The root-to-leaf path 4->0 represents the number 40.
    Therefore, sum = 495 + 491 + 40 = 1026.


    题目大意:
    将一棵树从root -> leaf的一个遍历当成一个整数;求各条 root -> leaf遍历的和;
    简单的树的遍历;下面给出两种思路:

    • 递归
    • 循环

    代码如下(Swift5):

    class Solution {
        // 递归
        // Runtime: 8 ms, faster than 100.00% of Swift online submissions for Sum Root to Leaf Numbers.
        // Memory Usage: 19.1 MB, less than 25.00% of Swift online submissions for Sum Root to Leaf Numbers.
        func sumNumbers(_ root: TreeNode?) -> Int {
            var result = 0
            func next(_ previous: Int, node: TreeNode?) {
                guard let node = node else { return }
                let current = previous * 10 + node.val
                if node.left == nil && node.right == nil {
                    result += current
                } else {
                    next(current, node: node.left)
                    next(current, node: node.right)
                }
            }
            next(0, node: root)
            return result
        }
        
        // 循环
        // Runtime: 12 ms, faster than 89.86% of Swift online submissions for Sum Root to Leaf Numbers.
        // Memory Usage: 18.9 MB, less than 50.00% of Swift online submissions for Sum Root to Leaf Numbers.
        func sumNumbers_2(_ root: TreeNode?) -> Int {
            typealias keyNode = (previous: Int, node: TreeNode)
            
            guard let root = root else { return 0 }
            
            var result = 0
            var keyNodes: [keyNode] = [(0, root)]
            while !keyNodes.isEmpty {
                
                var temp: [keyNode] = []
                for keyNode in keyNodes {
                    let node = keyNode.node
                    let current = keyNode.previous * 10 + node.val
                    
                    if node.left == nil, node.right == nil {
                        result += current
                    } else {
                        if let left = node.left {
                            temp.append((current, left))
                        }
                        if let right = node.right {
                            temp.append((current, right))
                        }
                    }
                }
                keyNodes = temp
            }
            return result
        }
    }
    

    相关文章

      网友评论

          本文标题:算法练习--LeetCode--129. Sum Root to

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