###### Middle-题目46：129. 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.

public class Solution {
private int currentTotal = 0;
public int sumNumbers(TreeNode root) {
dfs(root,0);
return currentTotal;
}
private void dfs(TreeNode node, int currentSum) {
if(node == null)
return;
else if(isLeaf(node)) {
currentSum+=node.val;
currentTotal+=currentSum;
}
else {
currentSum+=node.val;
dfs(node.left,currentSum*10);
dfs(node.right,currentSum*10);
}
}
private boolean isLeaf(TreeNode node) {
return node.left==null && node.right==null;
}
}

1ms,beats 28.93%,众数1ms,60.87%

#### 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 129. Sum Root to Leaf Numbers-根到叶子之和|bfs|dfs|递归|非递归

2016-06-05 13:21:50

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

2013-11-03 09:15:06

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

2017-01-15 21:54:42

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

2016-11-04 18:27:25

#### 129. Sum Root to Leaf Numbers**

2016-12-24 15:13:04

#### 129. Sum Root to Leaf Numbers

2015-08-14 21:06:34

## 不良信息举报

Middle-题目46：129. Sum Root to Leaf Numbers