zoukankan      html  css  js  c++  java
  • LeetCode 637. Average of Levels in Binary Tree

    原题链接在这里: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 }
  • 相关阅读:
    centos 用户指定目录访问
    centos FTP 用户指定目录禁用上级目录
    centos下SVN搭建多个库文件总汇
    listview点击checkbox,修改值
    C#转成时间格式
    nmap 查看主机上开放的端口
    xargs、管道、exec区别
    OSI七层模型,作用及其对应的协议
    linux面试题(重点)
    数据库备份还原 mysqldump
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7508152.html
Copyright © 2011-2022 走看看