/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int sumNumbers(TreeNode root) {
long[] temp = new long[2];
dfs(root, temp);
return (int)temp[0];
}
public void dfs(TreeNode root, long[] temp) {
if(root == null) return;
if(root.left == null && root.right == null) {
temp[0] += temp[1] * 10 + root.val;
}
temp[1] = temp[1] * 10 + root.val;
dfs(root.left, temp);
dfs(root.right, temp);
temp[1] = (temp[1] - root.val) / 10;
}
}