``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode *> queue;
int level = 0;
if(root) queue.push(root);

while(!queue.empty())
{
level += 1;
int n = queue.size();
for(int i = 0;i < n;i++)
{
TreeNode * elem = queue.front();
if(elem->left == NULL && elem->right == NULL)
return level;
if(elem->left) queue.push(elem->left);
if(elem->right) queue.push(elem->right);
queue.pop();
}
}

return level;
}
};``````

``````Runtime: 324 ms, faster than 54.93% of C++ online submissions for Minimum Depth of Binary Tree.
Memory Usage: 144.4 MB, less than 5.02% of C++ online submissions for Minimum Depth of Binary Tree.``````

``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if(root == NULL) return 0;
int left = minDepth(root->left);
int right = minDepth(root->right);

return (left == 0 || right == 0) ? left + right + 1:min(left,right) + 1;
}
};``````

``````Runtime: 344 ms, faster than 26.69% of C++ online submissions for Minimum Depth of Binary Tree.
Memory Usage: 146.5 MB, less than 5.02% of C++ online submissions for Minimum Depth of Binary Tree.``````