leetcode111_二叉树最小深度
public int minDepth(TreeNode root) {
int depth = 0;
if(root == null) return depth;
Deque<TreeNode> q = new LinkedList();
q.offer(root);
while(!q.isEmpty()) {
int size = q.size();
depth++;
for(int i = 0; i < size; i++) {
TreeNode node = q.poll();
if(node.left == null && node.right == null) return depth;
if(node.left != null) q.offer(node.left);
if(node.right != null) q.offer(node.right);
}
}
return depth;
}

浙公网安备 33010602011771号