[LeetCode]题解(python):129-Sum Root to Leaf Numbers
题目来源:
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的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【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 ...
- [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 ...
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- 基于eclipse的mybatis映射代码自动生成的插件
基于eclipse的mybatis映射代码自动生成的插件 分类: JAVA 数据库 工具相关2012-04-29 00:15 2157人阅读 评论(9) 收藏 举报 eclipsegeneratori ...
- WPF 采用Border创建圆角
通过设置可以创建圆角border的CornerRadius属性其边框呈现圆角样式 代码: <Border Height="50" Background="Red&q ...
- BZOJ 2716 Violet 3 天使玩偶 CDQ分治
题目大意:初始给定平面上的一个点集.提供两种操作: 1.将一个点增加点集 2.查询距离一个点最小的曼哈顿距离 K-D树是啥...不会写... 我仅仅会CDQ分治 对于一个询问,查询的点与这个点的位置关 ...
- 散列表的实现 -- 数据结构与算法的javascript描述 第八章
散列表(哈希表 散列是一种常用的数据存储技术,散列后的数据可以快速地插入或取用. 散列表需要一个散列值(key)来存储指定数据,取数据也是依靠此. 散列值可以依靠计算数据的 ASCII码来获得,但是这 ...
- Mac OS X下Maven的安装与配置
Mac OS X 安装Maven: 下载 Maven, 并解压到某个目录.例如/Users/robbie/apache-maven-3.3.3 打开Terminal,输入以下命令,设置Maven cl ...
- Labview学习之远程控制VI
Labview学习之远程控制VI 从LabVIEW 6.1开始,LabVIEW集成了Remote Panels技术,允许用户直接在客户端计算机上打开并操作位于服务器端计算机上的VI的前面 ...
- 如何创建一个简单的struts2程序
如何创建一个简单的Struts2程序 “计应134(实验班) 凌豪” 1.创建一个新的Web项目test(File->new->Web Project) 2.Struts2框架的核心配置文 ...
- iOS 开发技巧
快速移除键盘 之前一直是讲view的父类改成control,然后加一个全屏按钮,点击让键盘消失.这个方法要写好多代码,现在一个比较好的方法是重写一个下面的方法: - (void)touchesBega ...
- MYSQL alter procedure alter function 它们只可以更改过程的特性,不可以更改过程的逻辑。
例子: delimiter // create procedure proc_a(in numberA int) 这样create procedure 是正确的 begin select number ...
- Linux 硬盘、网卡
根据硬盘接口的不同,在Liunx中会有不同的命名 IDE 接口的硬盘会被叫成: hda,hdb,hdc (hd -- hard disk) hda 表示第一块硬盘,hdb表示第二块硬盘! 一般来说我们 ...