题目:
code 如下:
class Solution {
public int sumNumbers(TreeNode root) {
return sumNumbersDg(root, 0);
}
private int sumNumbersDg(TreeNode cur, int sum) {
if (cur == null) {
return 0;
}
if (cur.left == null && cur.right == null) {
return sum + cur.val;
}
int left = sumNumbersDg(cur.left, sum * 10 + cur.val);
int right = sumNumbersDg(cur.right, sum * 10 + cur.val);
return left + right;
}
}