博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法练习--LeetCode--129. Sum Root to Leaf Numbers; Runtime: 8 ms100%
阅读量:7222 次
发布时间:2019-06-29

本文共 2810 字,大约阅读时间需要 9 分钟。

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   3Output: 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   1Output: 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

你可能感兴趣的文章
java序列化
查看>>
谈谈NITE 2的第一个程序HandViewer
查看>>
VS2008 未响应 假死
查看>>
html5、css3及响应式设计入门
查看>>
Win10還原成最乾淨的狀態
查看>>
Java_InvokeAll_又返回值_多个线程同时执行,取消超时线程
查看>>
SaltStack作业
查看>>
单例设计
查看>>
springboot+缓存
查看>>
/*10个filter的属性*/ ---毛玻璃效果
查看>>
折半查找习题解答
查看>>
51单片机的P1
查看>>
[32]JSON
查看>>
3689: 异或之
查看>>
字符串模式匹配KMP算法
查看>>
Android Drawable和Bitmap图片之间转换
查看>>
Debian 8 安装 Nvidia 显卡驱动
查看>>
nginx静态文件访问
查看>>
SharePoint 2013中的默认爬网文件扩展名和分析文件类型
查看>>
c#-冒泡排序-算法
查看>>