binary-tree-level-order-traversal.sh — zsh

Binary Tree Level Order Traversal

medium
binary-treebreadth-first-searchqueue

Return the values of binary tree root level by level from left to right.

Constraints

  • 0 <= number of nodes <= 2000
  • -1000 <= node value <= 1000

Follow-up

How can queue length mark the boundary between levels?

Examples
Example 1
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
Example 2
Input: root = [1]
Output: [[1]]
Example 3
Input: root = []
Output: []
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.