Level Order Traversal

Binary Trees Theory/Traversals Easy

Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).

Examples:

Input : root = [3, 9, 20, null, null, 15, 7]

Output : [ [3] , [9, 20] , [15, 7] ]

Explanation :


Input : root = [1, 4, null, 4 2]

Output : [ [1] , [4] , [4, 2] ]

Explanation :


Input : root = [5, 1, 2, 8, null, 4, 5, null, 6]

Constraints

  • 0 <= Number of Nodes <= 2000
  • -1000 <= Node.val <= 2000

Hints

  • A queue (FIFO) is the best way to handle level order traversal since we process nodes level by level.
  • Push the root node into the queue. While the queue is not empty, process all nodes at the current level, then enqueue their left and right children for the next level.

Company Tags

Walmart Morgan Stanley Freshworks Texas Instruments Bloomberg Deloitte Square Chewy Zoho Electronic Arts KPMG Bungie Unity Technologies Etsy Optum Wayfair Nutanix Pinterest Western Digital MongoDB Roche Roblox Teladoc Health Mastercard Salesforce Google Microsoft Amazon Meta Apple Netflix Adobe