BFS | Minimum Depth OF BT | EASY APPROACH
class Solution {//BFS
    public int minDepth(TreeNode root) {
        if(root==null)
            return 0;
        Queue<TreeNode> que = new ArrayDeque<>();
        que.offer(root);
        int level = 1;//Want to return the number of nodes
        while(!que.isEmpty()){
            int size = que.size();
            while(size-->0){
                TreeNode curr = que.poll();
                if(curr.left==null&&curr.right==null)
                    return level;
                if(curr.left!=null)
                    que.offer(curr.left);
                if(curr.right!=null)
                    que.offer(curr.right);
            }
            level++;
        }
        return 0;
    }
}//1ms 95%
Comments (0)