Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions problems/0102.二叉树的层序遍历.md
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,43 @@ public:
```

Java:
```java
class Solution {

public int minDepth(TreeNode root){
if (root == null) {
return 0;
}

Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int depth = 0;

while (!queue.isEmpty()){

int size = queue.size();
depth++;

TreeNode cur = null;
for (int i = 0; i < size; i++) {
cur = queue.poll();

//如果当前节点的左右孩子都为空,直接返回最小深度
if (cur.left == null && cur.right == null){
return depth;
}

if (cur.left != null) queue.offer(cur.left);
if (cur.right != null) queue.offer(cur.right);
}


}

return depth;
}
}
```


Python 3:
Expand Down