/** * 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(); 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 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); } } }