【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. Go知识点大纲

    目录 1. 基本介绍 2. 安装及配置 3. 变量 4. 常量 5. 数据类型 5.1 numeric(数字) 5.2 string(字符串) 5.3 array(数组) 5.4 slice(切片) ...

  2. cmd查看同一个局域网内电脑IP

    win+R,cmd  #快速打开cmd窗口 net view  #查看本地局域网内开启了哪些计算机共享  运行后可以看到已共享的计算机名称 net view ip  #查看对方局域网内开启了哪些共享 ...

  3. 重新整理 .net core 实践篇——— endpoint[四十七]

    前言 简单整理一些endpoint的一些东西,主要是介绍一个这个endpoint是什么. 正文 endpoint 从表面意思是端点的意思,也就是说比如客户端的某一个action 是一个点,那么服务端的 ...

  4. dubbo 协议的 K8s pod 存活探针配置

    背景 某项目采用微服务架构,dubbo 框架,K8s 方式部署. 其中 HTTP 协议由网关应用统一处理,大部分应用仅提供 dubbo 协议. 目标 应用某个实例(pod)状态异常时,尝试自动重启恢复 ...

  5. 日常Java 2021/9/26 (二柱升级版)

    package m; import java.util.Scanner;import java.util.Random; public class di_er { static int number= ...

  6. A Child's History of England.38

    CHAPTER 12 ENGLAND UNDER HENRY THE SECOND PART THE FIRST Henry Plantagenet, when he was but [only] t ...

  7. day07 MySQL索引事务

    day07 MySQL索引事务 昨日内容回顾 pymysql模块 # 链接数据库都是使用这个模块的 # 创建链接 import pymysql conn = pymysql.connect( host ...

  8. Flume(四)【配置文件总结】

    目录 一.Agent 二.Source taildir arvo netstat exec spooldir 三.Sink hdfs kafka(待续) hbase(待续) arvo logger 本 ...

  9. Identity Server 4 从入门到落地(八)—— .Net Framework 客户端

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  10. Oracle中常用的系统函数

    本文主要来梳理下Oracle中的常用的系统函数,掌握这些函数的使用,对于我们编写SQL语句或PL/SQL代码时很有帮助,所以这也是必须掌握的知识点. 本文主要包括以下函数介绍:1.字符串函数2. 数值 ...