算法练习--LeetCode--129. Sum Root to Leaf Numbers; Runtime: 8 ms100%

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
    }
}
复制代码

转载于:https://juejin.im/post/5cb1be8b6fb9a068676fd791

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值