package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

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

/**
 * 129. Sum Root to Leaf Numbers
 *
 * 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 static class Solution1 {
    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);
    }
  }

  public static class Solution2 {
    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);
    }
  }

}

@@ -134,459 +134,8 @@
 t;%0A%0A
-/**%0A * 129. Sum Root to Leaf Numbers%0A *%0A * Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.%0A%0A An example is the root-to-leaf path 1-%3E2-%3E3 which represents the number 123.%0A%0A Find the total sum of all root-to-leaf numbers.%0A%0A For example,%0A%0A 1%0A / %5C%0A 2   3%0A The root-to-leaf path 1-%3E2 represents the number 12.%0A The root-to-leaf path 1-%3E3 represents the number 13.%0A%0A Return the sum = 12 + 13 = 25.%0A%0A */%0A
 publ
@@ -150,16 +150,18 @@
  _129 %7B%0A
+  
   public
@@ -186,16 +186,20 @@
 tion1 %7B%0A
+    
     publ
@@ -231,32 +231,38 @@
 e root) %7B%0A      
+ 
+     
 if (root == null
@@ -265,32 +265,40 @@
 null) %7B%0A        
+     
+   
 return 0;%0A      
@@ -293,26 +293,38 @@
 rn 0;%0A      
+     
+ 
 %7D%0A
+      
       List%3CI
@@ -361,16 +361,22 @@
 List();%0A
+      
       df
@@ -413,24 +413,30 @@
 llNumbers);%0A
+      
       int su
@@ -448,16 +448,22 @@
 ;%0A      
+     
+ 
 for (int
@@ -477,24 +477,32 @@
 lNumbers) %7B%0A
+        
         sum 
@@ -509,32 +509,44 @@
 += i;%0A      
+ 
-%7D%0A
+     %7D%0A     
       
+ 
 return sum;%0A
@@ -545,27 +545,35 @@
 rn sum;%0A    
-%7D%0A%0A
+    %7D%0A%0A    
     private 
@@ -648,16 +648,22 @@
 %7B%0A      
+   
+   
 sb.appen
@@ -677,24 +677,30 @@
 val);%0A      
+   
+   
 if (root.lef
@@ -712,32 +712,40 @@
 null) %7B%0A        
+  
+      
 dfs(root.left, s
@@ -758,34 +758,46 @@
 Numbers);%0A      
+    
-%7D%0A
+  %7D%0A      
       if (root.r
@@ -812,32 +812,40 @@
 null) %7B%0A        
+   
+     
 dfs(root.right, 
@@ -859,34 +859,46 @@
 Numbers);%0A      
+     
+ 
 %7D%0A
+      
       if (root.l
@@ -942,16 +942,24 @@
         
+    
+    
 allNumbe
@@ -999,24 +999,30 @@
 ng()));%0A
+      
       %7D%0A
       sb
@@ -1009,24 +1009,30 @@
           %7D%0A
+      
       sb.del
@@ -1059,24 +1059,28 @@
 ) - 1);%0A
+    
     %7D%0A  
 %7D%0A%0A  pub
@@ -1071,19 +1071,23 @@
     %7D%0A  
+ 
+ 
 %7D%0A%0A
+  
   public
@@ -1112,24 +1112,28 @@
 tion2 %7B%0A    
+  
+  
 public int s
@@ -1161,24 +1161,30 @@
 ot) %7B%0A      
+   
+   
 return dfs(r
@@ -1192,27 +1192,35 @@
 ot, 0);%0A    
-%7D%0A%0A
+    %7D%0A%0A    
     private 
@@ -1249,24 +1249,30 @@
  int sum) %7B%0A
+      
       if (ro
@@ -1285,32 +1285,40 @@
 null) %7B%0A        
+    
+    
 return 0;%0A      
@@ -1313,26 +1313,38 @@
 rn 0;%0A      
+    
-%7D%0A
+  %7D%0A      
       if (ro
@@ -1384,32 +1384,40 @@
 null) %7B%0A        
+     
+   
 return sum * 10 
@@ -1430,26 +1430,38 @@
 .val;%0A      
-%7D%0A
+      %7D%0A      
       return
@@ -1541,17 +1541,23 @@
 l);%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%0A%7D%0A

refactor 129