题目来源:

  https://leetcode.com/problems/sum-root-to-leaf-numbers/


题意分析:

  一棵树,从跟节点到叶子节点比如1->2那么这个叶子代表12,计算所有叶子的和。


题目思路:

  这题主要是怎么计算叶子的数值。leaf = node.sum * 10 + leaf.val。记录所有叶子的数值,然后相加即可。


代码(python):

 # Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
import math
class Solution(object):
def sumNumbers(self, root):
"""
:type root: TreeNode
:rtype: int
"""
ans = []
def solve(sum,root):
if root:
sum += root.val
if root.right == None and root.left == None:
ans.append(sum)
if root.right:
solve(sum*10,root.right)
if root.left:
solve(sum*10,root.left)
solve(0,root)
res = 0
for i in ans:
res += i
return res

[LeetCode]题解(python):129-Sum Root to Leaf Numbers的更多相关文章

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

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  2. 【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)

    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. LeetCode OJ 129. Sum Root to Leaf Numbers

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

  6. 【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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

  10. 129. Sum Root to Leaf Numbers pathsum路径求和

    [抄题]: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a ...

随机推荐

  1. mac文件权限

    如何设置文件/或文件夹权限为777 进入终端,切换到指定目录,输入以下命令,后面添加你的文件名/目录名$sudo chmod -R 777 (文件名/目录名) 或 $chmod 777 ./test. ...

  2. oracle 快照(snapshot) 管理

     ----手工创建oracle 快照 BEGIN DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT (); END; / ---删除快照 详细快照信息能够查看视图 ...

  3. WCF初步学习

    一.理解面向服务(Service-Oriented-Architecture)    是指为了解决在Internet环境下业务集成的需要,通过连接能完成特定任务的独立功能实体实现的一种软件系统架构.S ...

  4. JavaScript 开发经验整理

    前言 今年接触了一个B/S的项目,总结了一些JavaScript开发经验,整理些有用的内容与大家分享. 本文会持续更新... 1.实现代码访问的控制 随着项目JavaScript代码库扩大,本应被控制 ...

  5. JS实现快排

    /*采用快排的方法排序,取第一个值为轴对数组进行分割排序,不断迭代后实现数组的排序*/ //定义分割函数 function partF(A,low, high){ var temp = A[low]; ...

  6. 浅谈HtmlUnit的使用

    一.htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容.项目可以模拟浏览器运行,被誉为java浏览器的开源实现.这个没有界面的浏览器,运行 ...

  7. 提交服务器 post get

    HttpRequest Post or Get // method --- WebRequestMethods.Http.Post 或 WebRequestMethods.Http.Get priva ...

  8. hive 学习笔记精简

    创建表: drop table t create table if not exists t (t string) partitioned by (log_date string) row forma ...

  9. 创建以及加载模块【nodejs第四篇】

    建立两个文件,文件一createModule.js ,文件二main.js createModule.js的代码,主要用于创建一个模块 /** * Created by Administrator o ...

  10. BZOJ 4197: [Noi2015]寿司晚宴( dp )

    N^0.5以内的质数只有8个, dp(i, j, k)表示用了前i个大质数(>N^0.5), 2人选的质数(<=N^0.5)集合分别为j, k时的方案数. 转移时考虑当前的大质数p是给哪个 ...