package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

 An example is the root-to-leaf path 1->2->3 which represents the number 123.

 Find the total sum of all root-to-leaf numbers.

 For example,

 1
 / \
 2   3
 The root-to-leaf path 1->2 represents the number 12.
 The root-to-leaf path 1->3 represents the number 13.

 Return the sum = 12 + 13 = 25.

 */
public class _129 {
    public int sumNumbers(TreeNode root) {
        if (root == null) {
            return 0;
        }
        List<Integer> allNumbers = new ArrayList();
        dfs(root, new StringBuilder(), allNumbers);
        int sum = 0;
        for (int i : allNumbers) {
            sum += i;
        }
        return sum;
    }

    private void dfs(TreeNode root, StringBuilder sb, List<Integer> allNumbers) {
        sb.append(root.val);
        if (root.left != null) {
            dfs(root.left, sb, allNumbers);
        }
        if (root.right != null) {
            dfs(root.right, sb, allNumbers);
        }
        if (root.left == null && root.right == null) {
            allNumbers.add(Integer.parseInt(sb.toString()));
        }
        sb.deleteCharAt(sb.length() - 1);
    }

    class MoreConciseVersion {
        public int sumNumbers(TreeNode root) {
            return dfs(root, 0);
        }

        private int dfs(TreeNode root, int sum) {
            if (root == null) {
                return 0;
            }
            if (root.left == null && root.right == null) {
                return sum * 10 + root.val;
            }
            return dfs(root.left, sum * 10 + root.val) + dfs(root.right, sum * 10 + root.val);
        }
    }

}

@@ -133,16 +133,56 @@
 st;%0A%0A/**
+%0A * 129. Sum Root to Leaf Numbers%0A *%0A * 
 Given a 
@@ -601,16 +601,50 @@
  _129 %7B%0A
+  public static class Solution1 %7B%0A
     publ
@@ -680,26 +680,24 @@
 ot) %7B%0A      
-  
 if (root == 
@@ -704,36 +704,32 @@
 null) %7B%0A        
-    
 return 0;%0A      
@@ -724,30 +724,26 @@
 rn 0;%0A      
-  %7D%0A  
+%7D%0A
       List%3CI
@@ -782,26 +782,24 @@
 st();%0A      
-  
 dfs(root, ne
@@ -836,18 +836,16 @@
 ;%0A      
-  
 int sum 
@@ -855,18 +855,16 @@
 ;%0A      
- 
- 
 for (int
@@ -878,28 +878,24 @@
 lNumbers) %7B%0A
-    
         sum 
@@ -902,30 +902,26 @@
 += i;%0A      
-  %7D%0A  
+%7D%0A
       return
@@ -1021,18 +1021,16 @@
 %7B%0A      
-  
 sb.appen
@@ -1038,26 +1038,24 @@
 (root.val);%0A
-  
       if (ro
@@ -1073,36 +1073,32 @@
 null) %7B%0A        
-  
-  
 dfs(root.left, s
@@ -1115,34 +1115,32 @@
 ers);%0A      
-  
 %7D%0A      
   if (root.r
@@ -1119,34 +1119,32 @@
 ;%0A      %7D%0A      
- 
- 
 if (root.right !
@@ -1145,36 +1145,32 @@
 ight != null) %7B%0A
-    
         dfs(root
@@ -1196,30 +1196,26 @@
 ers);%0A      
-  %7D%0A  
+%7D%0A
       if (ro
@@ -1263,20 +1263,16 @@
         
-    
 allNumbe
@@ -1312,18 +1312,16 @@
 ng()));%0A
-  
       %7D%0A
@@ -1326,18 +1326,16 @@
 %7D%0A      
-  
 sb.delet
@@ -1370,44 +1370,47 @@
   %7D%0A
-%0A    class MoreConciseVers
+  %7D%0A%0A  public static class Solut
 ion
+2
  %7B%0A
-    
     
@@ -1444,30 +1444,24 @@
 ode root) %7B%0A
-      
       return
@@ -1479,27 +1479,19 @@
 0);%0A    
-  
-  
 %7D%0A%0A
-    
     priv
@@ -1524,30 +1524,24 @@
  int sum) %7B%0A
-      
       if (ro
@@ -1558,32 +1558,24 @@
 ) %7B%0A        
-       
- 
 return 0;%0A  
@@ -1574,38 +1574,26 @@
 rn 0;%0A      
-      %7D%0A      
+%7D%0A
       if (ro
@@ -1637,32 +1637,24 @@
 ) %7B%0A        
-       
- 
 return sum *
@@ -1675,30 +1675,18 @@
 ;%0A      
-      %7D%0A      
+%7D%0A
       re
@@ -1770,23 +1770,17 @@
 l);%0A    
-    %7D%0A  
+%7D%0A
   %7D%0A%0A%7D%0A

refactor 129