LeetCode 637: Average of Levels in Binary Tree Solution

Master LeetCode problem 637 (Average of Levels in Binary Tree), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

637. Average of Levels in Binary Tree

Problem Explanation

Explanation

To solve this problem, we can perform a level order traversal of the binary tree while calculating the average value of nodes at each level. We will maintain a queue to keep track of nodes at each level and a separate list to store the average values. At each level, we calculate the average value by summing up the values of nodes at that level and dividing by the number of nodes at that level.

Algorithm:

  1. Initialize a queue to store nodes at each level and a list to store average values.
  2. Push the root node into the queue.
  3. While the queue is not empty, do the following:
    • Initialize variables sum and count to calculate the sum and number of nodes at the current level.
    • Iterate through the nodes at the current level:
      • Pop a node from the queue.
      • Update sum with the node's value.
      • Increment count by 1.
      • Push the node's children into the queue if they exist.
    • Calculate the average value for the current level and add it to the list of average values.
  4. Return the list of average values.

Time Complexity:

The time complexity of this algorithm is O(n), where n is the number of nodes in the binary tree.

Space Complexity:

The space complexity of this algorithm is O(n) as we are using a queue to perform the level order traversal.

Solution Code

import java.util.*;

class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> result = new ArrayList<>();
        if (root == null) {
            return result;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            double sum = 0;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                sum += node.val;
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            result.add(sum / size);
        }

        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 637 (Average of Levels in Binary Tree)?

This page provides optimized solutions for LeetCode problem 637 (Average of Levels in Binary Tree) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 637 (Average of Levels in Binary Tree)?

The time complexity for LeetCode 637 (Average of Levels in Binary Tree) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 637 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 637 in Java, C++, or Python.

Back to LeetCode Solutions