package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

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

/**Given a binary tree, collect a tree's nodes as if you were doing this:
 * Collect and remove all leaves, repeat until the tree is empty.

 Example:
 Given binary tree
     1
    / \
   2   3
  / \
 4  5
 Returns [4, 5, 3], [2], [1].

 Explanation:
 1. Removing the leaves [4, 5, 3] would result in this tree:

   1
  /
 2
 2. Now removing the leaf [2] would result in this tree:

 1
 3. Now removing the leaf [1] would result in the empty tree:

 []
 Returns [4, 5, 3], [2], [1].

 */
public class _366 {

    List<List<Integer>> result = new ArrayList<List<Integer>>();

    public List<List<Integer>> findLeaves(TreeNode root) {
        dfs(root);
        return result;
    }

    int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int level = Math.max(dfs(root.left), dfs(root.right)) + 1;
        if (result.size() < level) {
            result.add(new ArrayList<Integer>());
        }
        result.get(level - 1).add(root.val);
        return level;
    }

}

@@ -133,16 +133,58 @@
 st;%0A%0A/**
+%0A * 366. Find Leaves of Binary Tree%0A *%0A * 
 Given a 
@@ -681,16 +681,56 @@
 _366 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     List
@@ -772,27 +772,18 @@
 ist%3C
-List%3CInteger%3E
 %3E();%0A%0A
+    
     
@@ -845,16 +845,20 @@
         
+    
 dfs(root
@@ -864,24 +864,28 @@
 t);%0A        
+  
+  
 return resul
@@ -887,16 +887,20 @@
 result;%0A
+    
     %7D%0A%0A 
@@ -898,24 +898,28 @@
      %7D%0A%0A    
+  
+  
 int dfs(Tree
@@ -935,24 +935,28 @@
 ) %7B%0A        
+    
 if (root == 
@@ -971,24 +971,28 @@
             
+  
+  
 return 0;%0A  
@@ -989,34 +989,42 @@
 turn 0;%0A        
-%7D%0A
+    %7D%0A    
         int leve
@@ -1078,24 +1078,28 @@
  1;%0A        
+    
 if (result.s
@@ -1123,24 +1123,28 @@
             
+ 
+   
 result.add(n
@@ -1156,23 +1156,16 @@
 rayList%3C
-Integer
 %3E());%0A  
@@ -1170,26 +1170,32 @@
         
-%7D%0A
+    %7D%0A  
         
 result.g
@@ -1182,24 +1182,26 @@
 %7D%0A          
+  
 result.get(l
@@ -1229,24 +1229,28 @@
 l);%0A        
+    
 return level
@@ -1255,13 +1255,22 @@
 el;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

refactor 366