Trees

Problem shape

Beginner
8
3
10
1
6
9
14
Core idea

Use a queue to visit tree nodes one level at a time.

Step 1 / 5
Problem shape
Current lesson

Level Order Traversal Lab

Trees Beginner

Level Order Traversal

Welcome to the Level Order Traversal algorithm lab. Use a queue to visit tree nodes one level at a time. Use it when: Use when tree depth or per-level grouping matters. The time complexity is O(n), and the space complexity is O(n). The pseudocode is: queue.push(root) while queue not empty node = queue.pop() visit node push node.left and node.right if present As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n)

Space

O(n)

Pseudocode

queue.push(root)
while queue not empty
  node = queue.pop()
  visit node
  push node.left and node.right if present
Open deeper structure lesson
1.0x