Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 301026b

Browse files
Merge pull request youngyangyang04#632 from ColorQian/master
添加 0102.二叉树的层序遍历.md文件 中的 111.二叉树的最小深度 的java代码
2 parents 68159f8 + f07fb2f commit 301026b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎problems/0102.二叉树的层序遍历.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,43 @@ public:
16231623
```
16241624
16251625
Java:
1626+
```java
1627+
class Solution {
1628+
1629+
public int minDepth(TreeNode root){
1630+
if (root == null) {
1631+
return 0;
1632+
}
1633+
1634+
Queue<TreeNode> queue = new LinkedList<>();
1635+
queue.offer(root);
1636+
int depth = 0;
1637+
1638+
while (!queue.isEmpty()){
1639+
1640+
int size = queue.size();
1641+
depth++;
1642+
1643+
TreeNode cur = null;
1644+
for (int i = 0; i < size; i++) {
1645+
cur = queue.poll();
1646+
1647+
//如果当前节点的左右孩子都为空,直接返回最小深度
1648+
if (cur.left == null && cur.right == null){
1649+
return depth;
1650+
}
1651+
1652+
if (cur.left != null) queue.offer(cur.left);
1653+
if (cur.right != null) queue.offer(cur.right);
1654+
}
1655+
1656+
1657+
}
1658+
1659+
return depth;
1660+
}
1661+
}
1662+
```
16261663

16271664

16281665
Python 3:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /