[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 ...
随机推荐
- javascript第一课javascript:void(0);
在一些需要使用<a href="#"></a>的地方,把#号换成javascript:void(0); 可以防止在链接跳转到新页面,void(0)放里面的参 ...
- Ultra-QuickSort(归并排序)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 49267 Accepted: 18035 ...
- Effective JavaScript Item 36 实例状态仅仅保存在实例对象上
本系列作为EffectiveJavaScript的读书笔记. 一个类型的prototype和该类型的实例之间是"一对多"的关系.那么,须要确保实例相关的数据不会被错误地保存在pro ...
- HTML之学习笔记(十)表单元素
html表单元素的基本格式为(必须包含在form标签中)
- JavaScript之Ajax
简介:AJax是在2005年被Adaptive Path的Jesse James Garrett发明的,用户概括异步加载页面内容的技术---说通俗点,Ajax就是实现局部页面通过JavaScript与 ...
- ContentType 属性 MIME
".asf" = "video/x-ms-asf" ".avi" = "video/avi" ".doc&qu ...
- Building Workspace速度慢的原因
今天把ext3.0部署到web project很慢很慢,查了一下,这个当笔记. 转自http://blog.163.com/jong_cai/blog/static/87028045201311178 ...
- Performance tool httperf
httperf: A relatively well-known open source utility developed by HP, for Linux operating systems on ...
- MapReuce 编程总结-多MapReduce执行
学习hadoop,必不可少的就是写MapReduce程序,当然,对于简单的分析程序,我们只需一个MapReduce就能搞定,这里就不提单MapReuce的情况了,网上例子很多,大家可以百度Google ...
- K-Modes算法[聚类算法]
聚类算法k-Modes的实现 <?php /* *Kmodes算法(聚类算法的实现) */ /* *获取簇的数目 */ //----------------------------------- ...