语言:Python

描述:使用递归实现

     def getList(self, node):
if node is None:
return [] if node.left is None and node.right is None:
return [[node.val]] result = []
for item in self.getList(node.left):
result.append([node.val] + item) for item in self.getList(node.right):
result.append([node.val] + item) return result def getNumByList(self, lst):
result =
for item in lst:
result *=
result += item
return result def sumNumbers(self, root):
result = self.getList(root)
sum =
for item in result:
sum += self.getNumByList(item)
return sum

新的实现

 class Solution:
# @param root, a tree node
# @return an integer
def sumPath(self, node, value):
if node is None:
return value = value * + node.val
if (node.left is None and node.right is None):
self.sum += value self.sumPath(node.left, value)
self.sumPath(node.right, value) def sumNumbers(self, root):
self.sum =
self.sumPath(root, ) return self.sum

#Leet Code# Root to leaf的更多相关文章

  1. 23. 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 解题报告

    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 (2 solutions)

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  4. 【Leet Code】Palindrome Number

    Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...

  5. LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II

    1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...

  6. 【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】

    [129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...

  7. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  8. Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths)

    Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths) 这篇是DFS专题的第一篇,所以我会 ...

  9. Leet Code 771.宝石与石头

    Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S  ...

随机推荐

  1. 在Mybatis-spring中由于默认Autowired导致不能配置多个数据源的问题分析及解决

    在使用Mybatis中,通常使用接口来表示一个Sql Mapper的接口以及相对应的xml实现,而在spring的配置文件中,通常会使用MapperScannerConfigurer来达到批量扫描以及 ...

  2. MySQL 使用while语句向数据表中批量插入数据

    1.创建一张数据表 mysql> create table test_while ( -> id int primary key) charset = utf8; Query OK, ro ...

  3. 基于前端javascript的搜索功能

    发表于 2013/11/07 当在数据量不是很大,而且没有后端对应的功能接口的时候,一些简单的搜索功能基本上是前端去实现的,正好最近用到,写了一个,贴出来和大家分享: 功能描述: 按下键盘后及时搜索条 ...

  4. 基于heartbeat的单播方式实现tomcat高可用

    1.节点规划 在master.backup节点上添加eth0.eth1两网卡,具体添加过程,参考“基于VMware为CentOS 6.5配置两个网卡” 2.IP规划   master backup e ...

  5. ubuntu文件管理常用命令 分类: linux ubuntu 学习笔记 2015-07-02 16:57 29人阅读 评论(0) 收藏

    1.关闭防火墙:ufw disable 2.以.开头的表示隐藏文件 3..和..分别代表当前目录以及当前目录的父目录 4.显示当前用户所在目录pwd 5.touch创建空文件 6.mkdir创建新目录 ...

  6. linux根下目录详解及分区建议

    / 根目录    分区大小一定要充足,一般不小于5GB/bin,/usr/bin 普通用户使用命令    建议和/放一起/sbin,/usr/sbin 管理员使用命令/bin,/sbin 操作系统自身 ...

  7. JavaScript的DOM操作(一)

    DOM:文档对象模型 --树模型文档:标签文档,对象:文档中每个元素对象,模型:抽象化的东西 一:window: 属性(值或者子对象):opener:打开当前窗口的源窗口,如果当前窗口是首次启动浏览器 ...

  8. 第二篇:杂项之图像处理pillow

    杂项之图像处理pillow   杂项之图像处理pillow 本节内容 参考文献 生成验证码源码 一些小例子 1. 参考文献 http://pillow-cn.readthedocs.io/zh_CN/ ...

  9. like的性能问题

    使用like'%匹配的文字%',无法优化,因为索引起不到作用. 不过like'匹配的文字%',索引起作用.

  10. JDBC、Hibernate、Mybaites处理数据的流程及对DAO的理解

    以查询一个用户信息(id,name)为例: JDBC 1. 获取一个connection 2. 生成一个statement 3. 拼接SQL语句 4. 查询对象,获取结果集(假设已经找到我们需要的对象 ...