LeetCode - Easy - 637. Average of Levels in Binary Tree

该博客介绍了如何使用广度优先搜索(BFS)和深度优先搜索(DFS)方法计算二叉树各层节点的平均值。首先,通过BFS方法,利用队列逐层遍历并计算平均值;其次,采用DFS方法,通过记录层级信息和节点值来计算各级别的平均值。文章提供了详细的代码实现,并通过测试用例验证了两种方法的正确性,包括处理大型二叉树的情况。
摘要由CSDN通过智能技术生成

Topic

  • Tree

Description

https://leetcode.com/problems/average-of-levels-in-binary-tree/

Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 1 0 − 5 10^{-5} 105 of the actual answer will be accepted.

Example 1:

Input: root = [3,9,20,null,15,7]
Output: [3.00000,14.50000,11.00000]
Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
Hence return [3, 14.5, 11].

Example 2:

Input: root = [3,9,20,15,7]
Output: [3.00000,14.50000,11.00000]

Constraints:

  • The number of nodes in the tree is in the range [ 1 , 1 0 4 ] [1, 10^4] [1,104].
  • − 2 31 ⩽ N o d e . v a l ⩽ 2 31 − 1 -2^{31} \leqslant Node.val \leqslant 2^{31} - 1 231Node.val2311

Analysis

方法一:BFS

方法二:DFS

Submission

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import com.lun.util.BinaryTree.TreeNode;

public class AverageOfLevelsInBinaryTree {

	// 方法一:BFS
	public List<Double> averageOfLevels(TreeNode root) {
		LinkedList<TreeNode> queue = new LinkedList<>();

		queue.offer(root);
		List<Double> result = new ArrayList<>();

		while (!queue.isEmpty()) {

			int size = queue.size(), originSize = size;
			long sum = 0;

			for (int i = 0; i < size; i++) {
				TreeNode node = queue.poll();

				sum += node.val;

				if (node.left != null)
					queue.offer(node.left);

				if (node.right != null)
					queue.offer(node.right);

			}
			result.add(sum * 1.0 / originSize);
		}

		return result;
	}

	// 方法二:DFS
	public List<Double> averageOfLevels2(TreeNode root) {
		List<long[]> levelSumAndCounts = new ArrayList<>();
		averageOfLevels2(root, 0, levelSumAndCounts);
		return levelSumAndCounts.stream().map(a -> a[0] * 1.0 / a[1]).collect(Collectors.toList());
	}

	private void averageOfLevels2(TreeNode node, int level, List<long[]> levelSumAndCounts) {
		if (node == null)
			return;

		if (levelSumAndCounts.size() == level) {
			levelSumAndCounts.add(new long[] {node.val, 1});
		} else {
			long[] temp = levelSumAndCounts.get(level);
			temp[0] += node.val;
			temp[1]++;
		}

		averageOfLevels2(node.left, level + 1, levelSumAndCounts);
		averageOfLevels2(node.right, level + 1, levelSumAndCounts);
	}
}

Test

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Test;

import com.lun.util.BinaryTree;
import com.lun.util.BinaryTree.TreeNode;

public class AverageOfLevelsInBinaryTreeTest {

	private TreeNode bigRoot = BinaryTree.integers2BinaryTree(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //
			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2147483647,2147483647,2147483647,2147483643,2147483645,2147483646,2147483646, //
			2147483624,2147483646,2147483647,2147483646,2147483647,2147483647,2147483631,2147483647,2147483647,2147483647, //
			2147483647,2147483631,2147483647,2147483647,2147483647,2147483647,2147483646,2147483646,2147483614,2147483646, //
			2147483647,2147483646,2147483647,2147483646,2147483647,2147483646,2147483647,2147483646,2147483647,2147483646, //
			2147483647,2147483646,2147483647,2147483646,2147483647,2147483646,2147483647,2147483646,2147483647,2147483646, //
			2147483647,2147483644,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647, //
			2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647, //
			2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644, //
			2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647, //
			2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483642,2147483647,2147483647, //
			2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647, //
			2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647, //
			2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636, //
			2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647, //
			2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647, //
			2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647, //
			2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647, //
			2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647, //
			2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647, //
			2147483647,2147483647,2147483642,2147483642,2147483647,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483647,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483647,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //
			2147483642,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483638,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //
			-2147483642,-2147483643,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //
			-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483643, //
			-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //
			-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483643,-2147483642,-2147483642, //
			-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //
			-2147483642,-2147483642,-2147483642,-2147483642,-2147483643,-2147483642,-2147483642,-2147483642,-2147483642, //
			-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //
			-2147483642,-2147483642,-2147483643,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //
			-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //
			-2147483643,-2147483642,-2147483642,-2147483642,-2147483642,-2147483598,-2147483647,-2147483647,-2147483647, //
			-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647, //
			-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645, //
			-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647, //
			-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647, //
			-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647, //
			-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //
			-2147483644,-2147483647,-2147483647,-2147483644,-2147483600,-2147483646,-2147483646,-2147483646,-2147483646, //
			-2147483646,-2147483646,-2147483646,-2147483646,-2147483646,-2147483646,-2147483646,-2147483647,-2147483647, //
			-2147483525,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483647,-2147483646,-2147483647, //
			-2147483336);

	@Test
	public void test() {
		AverageOfLevelsInBinaryTree obj = new AverageOfLevelsInBinaryTree();

		double delta = 10e-5;
		double[] expected = {3.00000, 14.50000, 11.00000};
		
		List<Double> result1 = obj.averageOfLevels(BinaryTree.integers2BinaryTree(3, 9, 20, null, 15, 7));
		List<Double> result2 = obj.averageOfLevels(BinaryTree.integers2BinaryTree(3, 9, 20, 15, 7));

		for(int i = 0; i < expected.length; i++) {
			assertEquals(expected[i], result1.get(i), delta);
			assertEquals(expected[i], result2.get(i), delta);
		}
		

		List<Double> result3 = obj.averageOfLevels(BinaryTree.integers2BinaryTree(2147483647, 2147483647, 2147483647));
		double[] expected3 = {2147483647.00000, 2147483647.00000};
		
		for(int i = 0; i < expected3.length; i++) {
			assertEquals(expected3[i], result3.get(i), delta);
		}
		
		List<Double> result4 = obj.averageOfLevels(bigRoot);
		double[] expected4 = {0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000};
		
		for(int i = 0; i < expected4.length; i++) {
			assertEquals(expected4[i], result4.get(i), delta);
		}
	}
	
	@Test
	public void test2() {
		AverageOfLevelsInBinaryTree obj = new AverageOfLevelsInBinaryTree();
		
		double delta = 10e-5;
		double[] expected = {3.00000, 14.50000, 11.00000};
		
		List<Double> result1 = obj.averageOfLevels2(BinaryTree.integers2BinaryTree(3, 9, 20, null, 15, 7));
		List<Double> result2 = obj.averageOfLevels2(BinaryTree.integers2BinaryTree(3, 9, 20, 15, 7));
		
		for(int i = 0; i < expected.length; i++) {
			assertEquals(expected[i], result1.get(i), delta);
			assertEquals(expected[i], result2.get(i), delta);
		}
		
		
		List<Double> result3 = obj.averageOfLevels2(BinaryTree.integers2BinaryTree(2147483647, 2147483647, 2147483647));
		double[] expected3 = {2147483647.00000, 2147483647.00000};
		
		for(int i = 0; i < expected3.length; i++) {
			assertEquals(expected3[i], result3.get(i), delta);
		}
		
		List<Double> result4 = obj.averageOfLevels2(bigRoot);
		double[] expected4 = {0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000};
		
		for(int i = 0; i < expected4.length; i++) {
			assertEquals(expected4[i], result4.get(i), delta);
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值