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
    }
}

@@ -338,15 +338,72 @@
     
-// TODO
+IntRef sum = new IntRef();%0A        sumRootToLeaf(root, %22%22, sum);
 %0A   
@@ -414,17 +414,23 @@
  return 
-0
+sum.val
 ;%0A    %7D%0A
@@ -435,29 +435,30 @@
 %7D%0A%0A    p
-ublic
+rivate
  static 
 void mai
@@ -453,201 +453,189 @@
 tic 
-void main(String%5B%5D args) %7B%0A        Problem1022 prob = new Problem1022();%0A
+class IntRef %7B%0A        private int val;
 %0A    
+%7D%0A%0A
     
-TreeNode root = new TreeNode(1);%0A        root.left = new TreeNode(0);%0A        root.left.left = new TreeNode(0);
+private static void sumRootToLeaf(TreeNode root, String s, IntRef sum) %7B%0A        if (root == null) %7B%0A            return;%0A        %7D
 %0A   
@@ -635,24 +635,28 @@
   %7D%0A        
+if (
 root.left.ri
@@ -652,24 +652,40 @@
 oot.left
+ == null && root
 .right =
  new Tre
@@ -680,210 +680,215 @@
 ht =
+=
  n
-ew TreeNode(1);%0A        root.right = new TreeNode(1);%0A        root.right.left = new TreeNode(0);%0A        root.right.right = new TreeNode(1);%0A        System.out.println(prob.sumRootToLeaf(root)); // 22
+ull) %7B%0A            int n = Integer.parseInt(s + root.val, 2);%0A            sum.val += n;%0A        %7D%0A        sumRootToLeaf(root.left, s + root.val, sum);%0A        sumRootToLeaf(root.right, s + root.val, sum);
 %0A   

Solve problem 1022