/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassSolution{publicintsumNumbers(TreeNoderoot){returnsum(root,0);}publicintsum(TreeNoden,ints){if(n==null)return0;if(n.right==null&&n.left==null)returns*10+n.val;returnsum(n.left,s*10+n.val)+sum(n.right,s*10+n.val);}}
Tranverse the tree and record the value for each path