/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int SumNumbers(TreeNode root)
{
if(root == null){
return 0;
}
var ret = new List<string>();
Sum(root, "",ref ret);
var s = 0;
for(var i = 0;i < ret.Count; i++){
s += int.Parse(ret[i]);
}
return s;
}
private void Sum(TreeNode root, string s,ref List<string> result)
{
if(root.left == null && root.right == null){
result.Add(s + root.val);
return;
}
if(root.left != null){
Sum(root.left, s + root.val, ref result);
}
if(root.right != null){
Sum(root.right, s + root.val, ref result);
}
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode -- Sum Root to Leaf Numbers
原文:http://blog.csdn.net/lan_liang/article/details/49885323