LeetCode 637. Average of Levels in Binary Tree

本文介绍了一种解决LeetCode上二叉树层级平均值问题的方法。提供了两种解题思路,一种是使用BFS遍历,另一种是使用DFS并创建额外的节点类来维护各层的总和和计数。

原题链接在这里:https://leetcode.com/problems/average-of-levels-in-binary-tree/discuss/

题目:

Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.

Example 1:

Input:
    3
   / \
  9  20
    /  \
   15   7
Output: [3, 14.5, 11]
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].

Note:

  1. The range of node's value is in the range of 32-bit signed integer.

题解:

可以采用BFS通过queue的size结算每层level的个数. 挨个poll出来计算sum.

Time Complexity: O(n), n 是node 个数. 

Space: O(n).

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public List<Double> averageOfLevels(TreeNode root) {
12         List<Double> res = new ArrayList<Double>();
13         if(root == null){
14             return res;
15         }
16         
17         LinkedList<TreeNode> que = new LinkedList<TreeNode>();
18         que.add(root);
19         while(!que.isEmpty()){
20             int count = que.size();
21             double sum = 0.0;
22             for(int i = 0; i<count; i++){
23                 TreeNode tn = que.poll();
24                 sum += tn.val;
25                 if(tn.left != null){
26                     que.add(tn.left);
27                 }
28                 if(tn.right != null){
29                     que.add(tn.right);
30                 }
31             }
32             res.add(sum/count);
33         }
34         return res;
35     }
36 }

也可以使用DFS. create 新的class Node 来维护每层的sum 和count.

Time Complexity: O(n). Space: O(logn).

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     class Node{
12         double sum;
13         int count;
14         Node(double sum, int count){
15             this.sum = sum;
16             this.count = count;
17         }
18     }
19     
20     public List<Double> averageOfLevels(TreeNode root) {
21         List<Double> res = new ArrayList<Double>();
22         List<Node> nodes = new ArrayList<Node>();
23         
24         avaerageOfLevelsDfs(root, nodes, 0);
25         for(int i = 0; i<nodes.size(); i++){
26             res.add(nodes.get(i).sum/nodes.get(i).count);
27         }
28         return res;
29     }
30     
31     private void avaerageOfLevelsDfs(TreeNode root,  List<Node> nodes, int level){
32         if(root == null){
33             return;
34         }
35         
36         if(level == nodes.size()){
37             nodes.add(new Node(1.0 * root.val, 1));
38         }else{
39             nodes.get(level).sum += root.val;
40             nodes.get(level).count++;
41         }
42         
43         avaerageOfLevelsDfs(root.left, nodes, level+1);
44         avaerageOfLevelsDfs(root.right, nodes, level+1);
45     }
46 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/7508152.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值