class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class Solution { public int sumNumbers(TreeNode root) { if(root==null)return 0; return sumRoot(root,0); } private int sumRoot(TreeNode root, int sum) { if(root==null)return 0; sum=sum*10+root.val;//关键 if(root.left==null&&root.right==null)return sum; return sumRoot(root.left, sum)+sumRoot(root.right, sum);//左右都要递归哦 } }