1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| int ans = 0;
public int sumNumbers(TreeNode root) { dfs(root, 0); return ans; }
private void dfs(TreeNode root, int temp) { if (root == null) return; if (root.left == null && root.right == null) { ans += temp * 10 + root.val; return; } dfs(root.left, temp * 10 + root.val); dfs(root.right, temp * 10 + root.val); }
|