【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/sum-root-to-leaf-numbers/description/

题目描述:

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.

For example,

    1
/ \
2 3
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13. Return the sum = 12 + 13 = 25.

解题方法

这个题和Binary Tree Paths一模一样,前个题是求路径,这个题是把路径按照10的倍数拼接在一起,最后求和即可。

有个技巧就是res = 0当做参数传给函数,那么函数最后的结果不会影响到res,但是如果把res = [0]即可。

代码:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def sumNumbers(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root == None:
return 0
res = [0]
self.dfs(root, res, root.val)
return res[0] def dfs(self, root, res, path):
if root.left == None and root.right == None:
res[0] += path
if root.left != None:
self.dfs(root.left, res, path * 10 + root.left.val)
if root.right != None:
self.dfs(root.right, res, path * 10 + root.right.val)

日期

2018 年 2 月 25 日

【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)的更多相关文章

  1. [LeetCode] 129. Sum Root to Leaf Numbers 解题思路

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  2. LeetCode: Sum Root to Leaf Numbers 解题报告

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  3. [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  4. leetcode@ [129] Sum Root to Leaf Numbers (DFS)

    https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...

  5. Java for LeetCode 129 Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  6. leetcode 129. Sum Root to Leaf Numbers ----- java

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  7. Leetcode#129 Sum Root to Leaf Numbers

    原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...

  8. LeetCode 129. Sum Root to Leaf Numbers 动态演示

    树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...

  9. [LeetCode]129. Sum Root to Leaf Numbers路径数字求和

    DFS的标准形式 用一个String记录路径,最后判断到叶子时加到结果上. int res = 0; public int sumNumbers(TreeNode root) { if (root== ...

随机推荐

  1. 【模板】单源最短路径(Dijkstra)/洛谷P4779

    题目链接 https://www.luogu.com.cn/problem/P4779 题目大意 给定一个 \(n\) 个点 \(m\) 条边有向图,每个点有一个非负权值,求从 \(s\) 点出发,到 ...

  2. 运算符重载+日期类Date

    Hello,一只爱学习的鱼 大学学习C++运算符重载的时候,老师出了一道"运算符重载+类"的综合练习题,让我们来一起看看吧! 题目: 设计一个日期类Date,包括年.月.日等私有成 ...

  3. Qt最好用评价最高的是哪个版本?

    来源: http://www.qtcn.org/bbs/read-htm-tid-89455.html /// Qt4:    4.8.7      4.X 系列终结版本 Qt5 :   5.6 LT ...

  4. Docker快速上手入门

    Docker 什么是Docker? Docker就是一种虚拟化的技术 可以通过Docker快速的下载使用第三方技术,方便搭建环境 目的:Securely build,share and run any ...

  5. navicate连接Mysql5.7时,显示Access denied for user 'root'@'localhost' (using password: YES) 错误

    最近新装了Mysql5.7,按如下设置好了允许远程连接    (1)找到mysql配置文件并修改 sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 将bind-ad ...

  6. Js数组内对象去重

    let person = [ {id: 0, name: "小明"}, {id: 1, name: "小张"}, {id: 2, name: "小李& ...

  7. 基于 vue-cli 的 lib-flexible 适配

    基于 vue-cli3.0 的 lib-flexible 适配方案 第一步:下载安装相关依赖 第二步:创建 vue.config.js 文件并配置 第三步:在 main.js 中引入 lib-flex ...

  8. ubantu上编辑windows程序

    命令简记 cd $GOROOT/src cp -r $GOROOT /root/go1.4 CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash 操作 ...

  9. Linux系统下安装tomcat

    一.前置条件 安装tomcat需要先安装jdk,所以没有安装jdk同学,详见参考文章 二.Linux上安装tomcat 1. 下载Apache tomcat tomcat官网下载地址 在左边,可以选择 ...

  10. Mysql配置文件 4c8g优化

    目录 一.说明 二.配置 一.说明 以下配置适合4核8G及以下的配置,会让性能稍微提高1/3左右. 测试语句 mysqlslap -uroot -p123456 --concurrency=100 - ...