algorythms
Tree BFS
LC #102Medium

Binary Tree Level Order Traversal

Tree BFS
AmazonGoogleMetaMicrosoftBloomberg

Problem

Return the level-order (BFS) traversal of a binary tree as a list of lists.

treebfsqueue

Constraints

  • 0 ≤ n ≤ 2000
  • -1000 ≤ Node.val ≤ 1000

Example

Inputroot = [3, 9, 20, null, null, 15, 7]
Output[[3], [9, 20], [15, 7]]
Why

Level 0: [3], Level 1: [9,20], Level 2: [15,7]

Hints — reveal one at a time