【leetcode】【91.6】【M】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.

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def __init__(self):
self.res = 0
def dfs(self,root,summ):
if root == None:
return
summ = root.val + summ*10

if root.left:
self.dfs(root.left,summ)
if root.right:
self.dfs(root.right,summ)
if root.left == None and root.right == None:
self.res += summ
#print root.val,summ

def sumNumbers(self, root):

self.dfs(root,0)
#print self.res
return self.res
"""
:type root: TreeNode
:rtype: int
"""



LeetCode(129)SumRoottoLeafNumbers

2014-02-28 15:57:49

Sum Root to Leaf Numbers -- LeetCode

2014-04-04 09:20:44

LeetCode 129:Sum Root to Leaf Numbers

2015-12-31 14:30:09

【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers（所有根到叶子结点组组成的数字相加）】

2015-08-15 06:11:18

leetcode Sum Root to Leaf Numbers 详细思考分析过程

2013-11-03 09:15:06

129. Sum Root to Leaf Numbers--注意非递归的方法

2017-01-15 21:54:42

leetcode 129. Sum Root to Leaf Numbers-根到叶子之和|bfs|dfs|递归|非递归

2016-06-05 13:21:50

二叉树遍历129. Sum Root to Leaf Numbers

2016-11-04 18:27:25

【LeetCode】Sum Root to Leaf Numbers 解题报告

2014-09-22 17:14:49

LEETCODE: Sum Root to Leaf Numbers

2015-01-03 16:15:40

不良信息举报

【leetcode】【91.6】【M】Sum Root to Leaf Numbers