Level Averages in a Binary Tree (easy)
Problem Statement
Given a binary tree, populate an array to represent the averages of all of its levels.
Example 1:
Example 2:
Try it yourself
Try solving this question here:
import java.util.*;class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}};class LevelAverage {public static List<Double> findLevelAverages(TreeNode root) {List<Double> result = new ArrayList<>();// TODO: Write your code herereturn result;}public static void main(String[] args) {TreeNode root = new TreeNode(12);root.left = new TreeNode(7);root.right = new TreeNode(1);root.left.left = new TreeNode(9);root.left.right = new TreeNode(2);root.right.left = new TreeNode(10);root.right.right = new TreeNode(5);List<Double> result = LevelAverage.findLevelAverages(root);System.out.print("Level averages are: " + result);}}
Solution
This problem follows the Binary Tree Level Order Traversal pattern. We can follow the same BFS approach. The only difference will be that instead of keeping track of all nodes of a level, we will only track the running sum of the values of all nodes in each level. In the end, we will append the average of the current level to the result array. ...