Breadth-First Search pattern

01 Matrix

Given a matrix of 0s and 1s, return a matrix of the same size where each cell holds the distance to the nearest 0, with distance measured between 4-directionally adjacent cells. Watch it run step by step, then write your own pseudocode and get it checked by AI.

  • LeetCode 542
  • Medium
  • 1 approach

Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.

Breadth-First SearchLeetCode 542

Medium

The approach

  1. 01 Multi-source BFS from all zeros O(R·C)O(R·C) space
Played step by step, with its time and space.

What you will learn

  • How to read 01 Matrix and spot the breadth-first search pattern in it
  • Multi-source BFS from all zeros: O(R·C) time, O(R·C) space
  • The pseudocode for each approach, its line lit as the animation runs
  • Practice: write your own pseudocode and get it checked

More Breadth-First Search

See 01 Matrix run, step by step

Sign in and this page opens on the animation, with all 8 Breadth-First Search problems beside it.

DSA comes with the all labs plan and every ChaiCode batch.