Leetcode Problem 102. Binary Tree Level Order Traversal

102. Binary Tree Level Order Traversal

Leetcode Solutions

Breadth-First Search (BFS) Iterative Approach

  1. Check if the root is null, return an empty list if true.
  2. Initialize a queue to hold nodes at the current level and add the root node to it.
  3. Initialize an output list to hold the level order traversal result.
  4. While the queue is not empty: a. Create a temporary list to store the values of nodes at the current level. b. Record the number of nodes at the current level (queue size). c. Iterate over the number of nodes at the current level: i. Dequeue the front node from the queue. ii. Add the node's value to the temporary list. iii. Enqueue the node's left and right children if they are not null. d. Add the temporary list to the output list.
  5. Return the output list.
UML Thumbnail

Depth-First Search (DFS) Recursive Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...