# C++实现LeetCode(111.二叉树的最小深度)

[LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度 Given a binary tree, find its minimum depth.

## [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

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: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

3

/ \

9  20

/  \

15   7

return its minimum depth = 2.

```class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) return 0;
if (!root->left) return 1 + minDepth(root-编程客栈>right);
if (!root->right) return 1 + minDepth(root->left);
return 1 + min(minDepth(root->left), minDepth(root->right));
}
};```

```class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) return 0;
int res = 0;
queue<TreeNode*> q{{root}};
while (!q.empty()) {
++res编程客栈;
for (int i = q.size(); i > 0; --i) {
auto t = q.front(); q.pop();
if (!t->left && !t->righwww.cppcns.comt) return res;
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
}
return -1;
}
};```

github 同步地址：

https://github.com/grandyang/leetcode/issues/111

Binary Tree Level Order Traversal

Maximum Depth of Binary Tree

https://leetcode.com/problems/minihttp://www.cppcns.commum-depth-of-binary-tree/

https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/36153/My-concise-c%2B%2B-solution

https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/36071/BFS-C%2B%2B-8ms-Beats-99.94-submissions