#Leet Code# Root to leaf
语言: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的更多相关文章
- 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 ...
- 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 ...
- 【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 ...
- 【Leet Code】Palindrome Number
Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...
- 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 ...
- 【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】
[129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...
- Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
- Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths)
Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths) 这篇是DFS专题的第一篇,所以我会 ...
- Leet Code 771.宝石与石头
Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S ...
随机推荐
- java 反射(reflect)总结,附对象打印工具类
java反射机制认知 java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取类的信息以及动态调用对象的方法的 ...
- android开发:@SuppressLint( NewApi )
这个是android带的lint工具提示的,lint官方的说法是 Improving Your Code with lint,应该是帮助提升代码的 ,如果不想用的话,可以右键点工程,然后在androi ...
- mysql 加载文本数据
可以配置导入哪几列,每个字段用什么隔开,每行用什么隔开,也可以单独设置某个字段的值. 详细看代码: LOAD DATA INFILE 'D:/aa.txt' INTO TABLE aa FIELDS ...
- JSP&Servlet学习手册
JSP&Servlet学习手册 沙琪玛 书 目录 JSP 指令... 3 书写方式... 3 指令列表... 3 JSP 内置对象... 3 内置对象特点... 3 常用内置对象... 3 o ...
- FragmentStatePagerAdapter.notifyDataSetChanged不刷新页面的解决的方法
公司做医疗产品的,显示操作用的是android.所以我就用上下两个部分大致是固定的,仅仅有中间会有6个页面的切换,当中会有两个用户的切换.即普通用户和管理员用户,图片能够大致展示一下 其他页面是同样的 ...
- (求助大牛)关于vs2010上的AVS代码bug问题~~
问题1:就是解码端,出现错误,找到bug所在地了,见下图: memcpy出错了,跳到下图了.可是错误显示的我不懂,求解释一下就ok了,小女子在此谢过了~~哎,调bug的能力弱爆了!! 大家看看吧~~是 ...
- oracle10 权限角色
管理权限和角色 介绍 这一部分我们主要看看oracle中如何管理权限和角色,权限和角色的区别在那里. 当刚刚建立用户时,用户没有任何权限,也不能执行任何操作,oracle数据库会自动创建一个方案, ...
- WGS84经纬度坐标与web墨卡托之间的转换【转】
第一种方法: //经纬度转Web墨卡托 dvec3 CMathEngine::lonLat2WebMercator(dvec3 lonLat) { dvec3 mercator; ; ); ; mer ...
- BI任务列表
了解点击流系统和pv/uv的相关计算 关于pv的那些事!! ···············································2014-09-10 homework做了些什 ...
- SharePoint Dialog 使用
SharePoint中弹出模态窗口对体验提高太大了 方法为: 父页面中调用子页面: function showDialog() { var options = { ...