package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**Given a binary tree, find its minimum depth.

 The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.*/

public class _111 {
/**We can solve this problem using both BFS and DFS:
 * DFS is to visit every single root to leaf path and return the shortest one.
 * BFS is to visit every level and return whenever we find the first leaf node.*/
    public int minDepth(TreeNode root) {
        if(root == null) return 0;
        int left = minDepth(root.left);
        int right = minDepth(root.right);
        if(left == 0) return right+1;
        if(right == 0) return left+1;
        return Math.min(left, right)+1;
    }
    
    public static void main(String[] args){
        _111 test = new _111();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        System.out.println(test.minDepth(root));
    }
    

    public int minDepth_BFS(TreeNode root) {
        if(root == null) return 0;
        Queue<TreeNode> q = new LinkedList();
        q.offer(root);
        int level = 0;
        while(!q.isEmpty()){
            level++;
            int size = q.size();
            for(int i = 0; i < size; i++){
                TreeNode curr = q.poll();
                if(curr.left != null) q.offer(curr.left);
                if(curr.right != null) q.offer(curr.right);
                if(curr.left == null && curr.right == null) return level;
            }
        }
        return level;
    }

}

@@ -135,16 +135,57 @@
 ue;%0A%0A/**
+%0A * 111. Minimum Depth of Binary Tree%0A * 
 Given a 
@@ -221,17 +221,18 @@
  depth.%0A
-%0A
+ *
  The min
@@ -338,16 +338,20 @@
 af node.
+%0A * 
 */%0A%0Apubl
@@ -366,19 +366,31 @@
  _111 %7B%0A
-/**
+    /**%0A     * 
 We can s
@@ -431,16 +431,20 @@
 nd DFS:%0A
+    
  * DFS i
@@ -514,16 +514,20 @@
 st one.%0A
+    
  * BFS i
@@ -597,19 +597,69 @@
 af node.
+%0A     
 */%0A
+%0A
+    public static class DFSSolution %7B%0A%0A    
     publ
@@ -691,34 +691,39 @@
 root) %7B%0A        
+    
 if
+ 
 (root == null) r
@@ -735,24 +735,28 @@
  0;%0A        
+ 
+   
 int left = m
@@ -767,32 +767,36 @@
 pth(root.left);%0A
+    
         int righ
@@ -829,18 +829,23 @@
         
+    
 if
+ 
 (left ==
@@ -860,17 +860,19 @@
 rn right
-+
+ + 
 1;%0A     
@@ -874,18 +874,23 @@
         
+    
 if
+ 
 (right =
@@ -905,17 +905,19 @@
 urn left
-+
+ + 
 1;%0A     
@@ -915,24 +915,28 @@
  1;%0A        
+    
 return Math.
@@ -955,22 +955,31 @@
 ght)
-+
+ + 
 1;%0A    
+  
-%7D
+  %7D%0A
 %0A    
+%7D%0A
 %0A   
@@ -997,243 +997,33 @@
 tic 
-void main(String%5B%5D args)%7B%0A        _111 test = new _111();%0A        TreeNode root = new TreeNode(1);%0A        root.left = new TreeNode(2);%0A        root.right = new TreeNode(3);%0A        System.out.println(test.minDepth(root));%0A    %7D%0A    %0A%0A
+class BFSSolution %7B%0A%0A    
     
@@ -1071,18 +1071,23 @@
         
+    
 if
+ 
 (root ==
@@ -1111,16 +1111,20 @@
         
+ 
+   
 Queue%3CTr
@@ -1149,24 +1149,28 @@
 nkedList();%0A
+    
         q.of
@@ -1184,24 +1184,28 @@
 t);%0A        
+    
 int level = 
@@ -1219,13 +1219,18 @@
     
+   
+ 
 while
+ 
 (!q.
@@ -1239,16 +1239,17 @@
 Empty())
+ 
 %7B%0A      
@@ -1254,16 +1254,20 @@
         
+ 
+   
 level++;
@@ -1275,24 +1275,28 @@
             
+ 
+   
 int size = q
@@ -1300,24 +1300,28 @@
 = q.size();%0A
+    
             
@@ -1323,16 +1323,17 @@
      for
+ 
 (int i =
@@ -1350,18 +1350,23 @@
 ze; i++)
+ 
 %7B%0A
+    
         
@@ -1407,34 +1407,39 @@
                 
+ 
+   
 if
+ 
 (curr.left != nu
@@ -1474,26 +1474,31 @@
             
+   
+ 
 if
+ 
 (curr.right 
@@ -1543,18 +1543,23 @@
         
+    
 if
+ 
 (curr.le
@@ -1618,26 +1618,33 @@
         
-%7D%0A
+    %7D%0A   
         
 %7D%0A      
@@ -1627,34 +1627,39 @@
    %7D%0A           
+ 
 %7D%0A
+    
         return l
@@ -1668,13 +1668,22 @@
 el;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

edit 111