Leetcode 129 Sum Root to Leaf Numbers
2016-05-17 10:12
681 查看
Given a binary tree containing digits from
An example is the root-to-leaf path
Find the total sum of all root-to-leaf numbers.
For example,
The root-to-leaf path
The root-to-leaf path
Return the sum = 12 + 13 =
0-9only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path
1->2->3which 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->2represents the number
12.
The root-to-leaf path
1->3represents the number
13.
Return the sum = 12 + 13 =
25.
class Solution(object): def sumNumbers(self, root): return self.sn(0,root) def sn(self,num,root): if not root: return 0 num = num*10 + root.val if not root.left and not root.right: return num return self.sn(num,root.left) + self.sn(num,root.right)
相关文章推荐
- java正则表达式匹配数字、字母、下划线、汉字
- freemarke之TemplateDirectiveModel详解
- iOS横竖屏转换问题
- UI控件笔记(六):UI之UINavigationController、navigationBar和UIView的封装
- Android Xutils 框架
- CSS3的REM设置字体大小
- 应用服务器知识(3)--Tomcat启动框架
- SQL中“有且仅有”一种状态的写法
- git多种操作
- 扎克伯格清华中文演讲 讲述使命、用心、向前看
- Type.GetType()与 typeof跨程序集反射
- mac pro 笔记本电脑 外接显示器 无图像 / 不显示
- 自定义滚动条
- Android布局-LinearLayout属性详解
- java 数字的内部表示
- 度量快速开发平台分页构建介绍
- eclipse中properties文件编码问题
- Ubuntu常用终端快捷键
- 代理协议委托一句话说明
- iOS开发-常用第三方开源框架介绍