package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;
/**Find the sum of all left leaves in a given binary tree.

 Example:

   3
  / \
 9  20
   /  \
  15   7

 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.*/
public class _404 {
    public int sumOfLeftLeaves(TreeNode root) {
        int result = 0;
        if (root == null) {
            return result;
        }
        return dfs(root, result, false);
    }

    private int dfs(TreeNode root, int result, boolean left) {
        if (root.left == null && root.right == null && left) {
            result += root.val;
            return result;
        }
        int leftResult = 0;
        if (root.left != null) {
            left = true;
            leftResult = dfs(root.left, result, left);
        }
        int rightResult = 0;
        if (root.right != null) {
            left = false;
            rightResult = dfs(root.right, result, left);
        }
        return leftResult + rightResult;
    }

    private class Solution2 {

        public int sumOfLeftLeaves(TreeNode root) {
            int sum = 0;
            if (root == null) {
                return sum;
            }
            if (root.left != null) {
                if (root.left.left == null && root.left.right == null) {
                    sum += root.left.val;
                } else {
                    sum += sumOfLeftLeaves(root.left);
                }
            }
            if (root.right != null) {
                sum += sumOfLeftLeaves(root.right);
            }
            return sum;
        }
    }
}

@@ -80,16 +80,50 @@
 ode;%0A/**
+%0A * 404. Sum of Left Leaves%0A *%0A * 
 Find the
@@ -333,16 +333,56 @@
  _404 %7B%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -425,24 +425,28 @@
 ) %7B%0A        
+    
 int result =
@@ -441,32 +441,36 @@
 int result = 0;%0A
+    
         if (root
@@ -485,32 +485,36 @@
 ) %7B%0A            
+    
 return result;%0A 
@@ -512,32 +512,36 @@
 result;%0A        
+    
 %7D%0A        return
@@ -530,24 +530,28 @@
   %7D%0A        
+ 
+   
 return dfs(r
@@ -571,16 +571,20 @@
 false);%0A
+    
     %7D%0A%0A 
@@ -578,32 +578,36 @@
 %0A        %7D%0A%0A    
+  
+  
 private int dfs(
@@ -649,32 +649,36 @@
 left) %7B%0A        
+    
 if (root.left ==
@@ -728,16 +728,20 @@
         
+   
+ 
 result +
@@ -748,24 +748,28 @@
 = root.val;%0A
+    
             
@@ -783,32 +783,36 @@
 result;%0A        
+    
 %7D%0A        int le
@@ -801,24 +801,28 @@
   %7D%0A        
+ 
+   
 int leftResu
@@ -821,32 +821,36 @@
 leftResult = 0;%0A
+    
         if (root
@@ -874,24 +874,28 @@
             
+  
+  
 left = true;
@@ -895,16 +895,20 @@
 = true;%0A
+    
         
@@ -946,32 +946,36 @@
  result, left);%0A
+    
         %7D%0A      
@@ -972,24 +972,28 @@
   %7D%0A        
+    
 int rightRes
@@ -993,32 +993,36 @@
 ightResult = 0;%0A
+    
         if (root
@@ -1047,24 +1047,28 @@
             
+ 
+   
 left = false
@@ -1069,16 +1069,20 @@
  false;%0A
+    
         
@@ -1130,34 +1130,42 @@
  left);%0A        
+   
+ 
 %7D%0A
+    
         return l
@@ -1185,24 +1185,34 @@
 ightResult;%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -1211,22 +1211,28 @@
 %7D%0A%0A    p
-rivate
+ublic static
  class S

refactor 404