728x90
class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int level = 1;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode curNode = queue.poll();
if (curNode.left == null && curNode.right == null) {
return level;
}
if (curNode.left != null) {
queue.offer(curNode.left);
}
if (curNode.right != null) {
queue.offer(curNode.right);
}
}
level++;
}
return level;
}
}
728x90
'알고리즘' 카테고리의 다른 글
[LeetCode] 144. Binary Tree Preorder Traversal (0) | 2023.12.05 |
---|---|
[LeetCode] 94. Binary Tree Inorder Traversal (0) | 2023.12.05 |
[LeetCode] 110. Balanced Binary Tree (0) | 2023.12.05 |
[LeetCode] 104. Maximum Depth of Binary Tree (0) | 2023.12.05 |
[LeetCode] 101. Symmetric Tree (0) | 2023.12.05 |