Leetcode Problem 637. Average of Levels in Binary Tree

637. Average of Levels in Binary Tree

Leetcode Solutions

Breadth First Search Approach

  1. Initialize an empty queue and enqueue the root node.
  2. While the queue is not empty, perform the following steps: a. Initialize a temporary list to hold the nodes of the next level. b. Initialize sum and count variables to 0. c. For each node in the current level (current queue): i. Add the node's value to sum. ii. Increment count. iii. Enqueue the node's children to the temporary list. d. Calculate the average for the current level as sum / count. e. Add the average to the result list. f. Replace the queue with the temporary list for the next level.
  3. Return the result list containing the averages.
UML Thumbnail

Depth First Search Approach

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...