原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/

题意:

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.

解题思路:看到二叉树,我们首先想到递归。比如一棵树如下:

                      1

                     /     \

                     2    3

                    /    \    /   \

                    4      5 6     7

     此题求和为sum=124+125+136+137,我们可以使用一个preSum变量来记录从根节点到节点父亲的路径,比如当我们递归的4时,preSum=12,递归到6时,preSum=13,这样就可以了。具体看代码。

代码:

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return an integer
def sum(self, root, preSum):
if root==None: return 0
preSum = preSum*10 + root.val
if root.left==None and root.right==None: return preSum
return self.sum(root.left, preSum)+self.sum(root.right, preSum) def sumNumbers(self, root):
return self.sum(root, 0)

[leetcode]Sum Root to Leaf Numbers @ Python的更多相关文章

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

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

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

  3. Leetcode 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: Sum Root to Leaf Numbers [129]

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

  5. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

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

  6. LeetCode :: Sum Root to Leaf Numbers [tree、dfs]

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

  7. [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

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

  8. LeetCode Sum Root to Leaf Numbers(DFS)

    题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...

  9. leetcode Sum Root to Leaf Numbers(所有路径之和)

    转载请注明来自souldak,微博:@evagle 观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的.那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最 ...

随机推荐

  1. annotation中的Autowired

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. springboot+thymeleaf 模板中传递参数误报错误 红色波浪线

    在使用IDEA开发SpringBoot项目时,使用了Thymeleaf模板引擎,在使用动态传参数时,HTML页面的动态参数出现了红色波浪线,情况如下如: 解决办法: 选择 File -> Set ...

  3. 求N!末尾所得数字0的个数

    题目:给定一个整数N ,那么N 的阶乘N !末尾有多少个0呢? 例如:N = 10,N! = 3628800,所以N!末尾就有2个零. 分析:如果直接先算出N!阶乘,很容易导致内存溢出.显然,直接算出 ...

  4. 【WIN10】Toast 通知

    DEMO下載:http://yunpan.cn/cFSLZQf5ePeTV  访问密码 1fce 1.顯示通知 使用xml確定通知內容. string xml = "<toast la ...

  5. Python学习笔记(八)—集合的学习

    集合(set)是一个无序不重复元素的序列. 基本功能是进行成员关系测试和删除重复元素. 1.集合的作用 它可以把一个列表中重复的数据去掉,而不需要你再写判断 可以做关系测试,比如说有两个科目,一个数学 ...

  6. Oracle | 给表和字段添加注释

    comment  on  column  表名.字段名   is  '注释内容'; comment on column OPERATOR_INFO.MAIN_OPER_ID is '归属操作员'; c ...

  7. [Dynamic Language] Python非子包引用

    Python非子包引用 python的搜索路径其实是一个列表(sys.path) 导入模块时python会自动去找搜索这个列表当中的路径,如果路径中存在要导入的模块文件则导入成功. 在项目中如果要引用 ...

  8. GCC 对C语言的扩展

    http://www.cnblogs.com/emituofo/archive/2012/07/20/2600995.html http://blog.csdn.net/andyhuabing/art ...

  9. gcc 内联汇编

    http://www.cnblogs.com/zhuyp1015/archive/2012/05/01/2478099.html

  10. PJ Naughter CSerialPort

    来自:http://www.codeproject.com/Articles/382/CSerialPort-v-Serial-Port-Wrapper 源码下载 Features Simple an ...