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.

Note:&nbsp;A leaf is a node with no children.

&nbsp;
Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: 2


Example 2:

Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [0, 105].
	-1000 &lt;= Node.val &lt;= 1000


class Solution(object):
    def minDepth(self, root):
        # Base case...
        # If the subtree is empty i.e. root is NULL, return depth as 0...
        if root is None:  return 0
        # Initialize the depth of two subtrees...
        leftDepth = self.minDepth(root.left)
        rightDepth = self.minDepth(root.right)
        # If the both subtrees are empty...
        if root.left is None and root.right is None:
            return 1
        # If the left subtree is empty, return the depth of right subtree after adding 1 to it...
        if root.left is None:
            return 1 + rightDepth
        # If the right subtree is empty, return the depth of left subtree after adding 1 to it...
        if root.right is None:
            return 1 + leftDepth
        # When the two child function return its depth...
        # Pick the minimum out of these two subtrees and return this value after adding 1 to it...
        return min(leftDepth, rightDepth) + 1;    # Adding 1 is the current node which is the parent of the two subtrees...
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int minDepth(TreeNode root) {
        if(root == null)
            return 0;
        
        int left = minDepth(root.left);
        int right = minDepth(root.right);
        if(root.left == null)
            return right+1;
        if(root.right == null)
            return left+1;
        return Math.min(left, right)+1;
    }
}
class Solution {
public:
    void maxlevel(TreeNode* root,int level,int &ans){
        if(!root)
            return ;

        if(!root->left && !root->right){
            ans=min(level,ans);
            return ;
        }

        maxlevel(root->left,level+1,ans);
        maxlevel(root->right,level+1,ans);
    }
    int minDepth(TreeNode* root) {
        if(!root)
            return 0;

        int ans=INT_MAX;
        maxlevel(root,0,ans);
        return ans+1;
    }
};
var minDepth = function(root) {
    if (!root){
        return 0
    }
    if(root.left && root.right){
        return Math.min(minDepth(root.left), minDepth(root.right)) + 1
    }
    if(root.right){
        return minDepth(root.right) + 1
    }
    if(root.left){
        return minDepth(root.left) + 1
    }
    return 1
};
Minimum Depth of Binary Tree