力扣每日一题,637. 二叉树的层平均值
题目描述
给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。
示例 1:
输入: 3 / \ 9 20 / \ 15 7 输出:[3, 14.5, 11] 解释: 第 0 层的平均值是 3 , 第1层是 14.5 , 第2层是 11 。因此返回 [3, 14.5, 11] 。
提示:
- 节点值的范围在32位有符号整数范围内。
思路
这题首先想到的是层次遍历,附上我的二叉树层次遍历模板
层次遍历模板1
模板1遍历结果是一个一维数组,无法区分每一层。
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
public static void main(String[] args) {
// 3
// / \
// 9 20
// / \
// 15 7
TreeNode root = new TreeNode(3);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);
new Solution().levelOrder(root);
}
void levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (!queue.isEmpty()) {
root = queue.poll();
System.out.print(root.val + " ");
if (root.left != null)
queue.offer(root.left);
if (root.right != null)
queue.offer(root.right);
}
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
层次遍历模板2
遍历结果是二维数组,可以区分每一层
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Solution1 {
public static void main(String[] args) {
// 3
// / \
// 9 20
// / \
// 15 7
TreeNode root = new TreeNode(3);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);
new Solution1().levelOrder(root);
}
void levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> temp = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
root = queue.poll();
temp.add(root.val);
if (root.left != null)
queue.offer(root.left);
if (root.right != null)
queue.offer(root.right);
}
res.add(temp);
}
System.out.println(res);
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
提交代码
为了求得每层得平均值,需套用可以区分层次的模板2
package 二叉树的层平均值;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import org.junit.Test;
public class Solution {
@Test
public void testLevelOrder() throws Exception {
TreeNode root = new TreeNode(3);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);
System.out.println(averageOfLevels(root));
}
@Test
public void testLevelOrder1() throws Exception {
TreeNode root = new TreeNode(2147483647);
root.left = new TreeNode(2147483647);
root.right = new TreeNode(2147483647);
System.out.println(averageOfLevels(root));
}
public List<Double> averageOfLevels(TreeNode root) {
List<Double> res = new ArrayList<Double>();
if (root == null) {
return res;
}
Deque<TreeNode> queue = new LinkedList<TreeNode>();
queue.offerLast(root);
while (!queue.isEmpty()) {
long sum = 0;
int size = queue.size();
for (int i = 0; i < size; i++) {
root = queue.pollFirst();
sum += root.val;
if (root.left != null) {
queue.offerLast(root.left);
}
if (root.right != null) {
queue.offerLast(root.right);
}
}
res.add((double) sum / size);
}
return res;
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}