111. Minimum Depth of Binary Tree (E)
https://leetcode.com/problems/minimum-depth-of-binary-tree/
Input: root = [3,9,20,null,null,15,7]
Output: 2Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
Solution:
Last updated
https://leetcode.com/problems/minimum-depth-of-binary-tree/
Input: root = [3,9,20,null,null,15,7]
Output: 2Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5Last updated
public int minDepth(TreeNode root) {
if(root==null)
{
return 0;
}
if(root.left==null)
{
return minDepth(root.right)+1;
}
if(root.right==null)
{
return minDepth(root.left)+1;
}
return Math.min(minDepth(root.left), minDepth(root.right))+1;
}int minDepth(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
// root 本身就是一层,depth 初始化为 1
int depth = 1;
while (!q.isEmpty()) {
int sz = q.size();
/* 将当前队列中的所有节点向四周扩散 */
for (int i = 0; i < sz; i++) {
TreeNode cur = q.poll();
/* 判断是否到达终点 */
if (cur.left == null && cur.right == null)
return depth;
/* 将 cur 的相邻节点加入队列 */
if (cur.left != null)
q.offer(cur.left);
if (cur.right != null)
q.offer(cur.right);
}
/* 这里增加步数 */
depth++;
}
return depth;
}