import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    public int sum = 0;
    /**
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    public int sumNumbers (TreeNode root) {

        if (root == null) {
            return 0;
        }
        treeDeep(root);
        return sum;
    }

    public void treeDeep (TreeNode root) {

        if (root == null) {
            return;
        }

        if (root.left == null && root.right == null) {
            sum += root.val;
            return;
        }

        if (root.left != null) {
            root.left.val += root.val * 10;
        }

        if (root.right != null) {
            root.right.val += root.val * 10;
        }

        sumNumbers(root.left);
        sumNumbers(root.right);

    }
}