package leetcode;

/**
 * https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/
 */
public class Problem1022 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public int sumRootToLeaf(TreeNode root) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1022 prob = new Problem1022();

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(0);
        root.left.left = new TreeNode(0);
        root.left.right = new TreeNode(1);
        root.right = new TreeNode(1);
        root.right.left = new TreeNode(0);
        root.right.right = new TreeNode(1);
        System.out.println(prob.sumRootToLeaf(root)); // 22
    }
}

package leetcode;

/**
 * https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/
 */
public class Problem1022 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public int sumRootToLeaf(TreeNode root) {
        IntRef sum = new IntRef();
        sumRootToLeaf(root, "", sum);
        return sum.val;
    }

    private static class IntRef {
        private int val;
    }

    private static void sumRootToLeaf(TreeNode root, String s, IntRef sum) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            int n = Integer.parseInt(s + root.val, 2);
            sum.val += n;
        }
        sumRootToLeaf(root.left, s + root.val, sum);
        sumRootToLeaf(root.right, s + root.val, sum);
    }
}

Solve problem 1022