1. 程式人生 > >637. Average of Levels in Binary Tree(一棵樹每層節點的平均數)(二叉樹的層序遍歷)

637. Average of Levels in Binary Tree(一棵樹每層節點的平均數)(二叉樹的層序遍歷)

sum i++ 頂點 span static 層遍歷 levels != new

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算法:

廣度優先遍歷:類似於樹的層序遍歷。將根放入隊列,一層層遍歷,將根彈出後,將左右孩子加入隊列。

1、樹的廣度優先遍歷:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 
*/ class Solution { public ArrayList<Integer> bfs(TreeNode root){ ArrayList<Integer> list=new ArrayList<Integer>(); if(root==null) return list; Queue<TreeNode> queue=new LinkedList<TreeNode>(); queue.add(root); while (!queue.isEmpty()){ TreeNode treeNode
=queue.poll(); if(treeNode.left!=null) queue.add(treeNode.left); if(treeNode.right!=null) queue.add(treeNode.right); list.add(treeNode.val); } return list; } }

2、圖的廣度優先遍歷

技術分享圖片

以s為頂點的鄰接表為:

  S -> A -> B

  A -> C -> D

  B -> E

class Solution {
static HashMap<Character,LinkedList<Character>> graph;//輸入的鄰接表
static HashMap<Character,Integer> dirt;//距離集合
public static void bfs(HashMap<Character,LinkedList<Character>> graph,
HashMap<Character,Integer> dirt,char start){
Queue<Character> queue=new LinkedList<Character>();
queue.add(start);//把起點加入隊列
dirt.put(start,0);//記錄每個節點距離頂點的距離
while (!queue.isEmpty()){
char top=queue.poll();//取出隊列頂的元素
int d=dirt.get(top)+1;
for(Character c:graph.get(top)){
if(!dirt.containsKey(c)){
dirt.put(c,d);
queue.add(c);
}
}
}
}
public static void main(String[] args) {
// s頂點的鄰接表
LinkedList<Character> list_s = new LinkedList<Character>();
list_s.add(‘A‘);
list_s.add(‘B‘);
LinkedList<Character> list_a = new LinkedList<Character>();
list_a.add(‘C‘);
list_a.add(‘D‘);
LinkedList<Character> list_b = new LinkedList<Character>();
list_b.add(‘D‘);
list_b.add(‘E‘);
LinkedList<Character> list_c = new LinkedList<Character>();
list_c.add(‘E‘);
LinkedList<Character> list_d = new LinkedList<Character>();
list_c.add(‘E‘);

//構造圖
graph = new HashMap<Character, LinkedList<Character>>();
graph.put(‘S‘, list_s);
graph.put(‘A‘, list_a);
graph.put(‘B‘, list_b);
graph.put(‘C‘, list_c);
graph.put(‘D‘, list_d);
graph.put(‘E‘, new LinkedList<Character>());

//調用
dirt = new HashMap<Character, Integer>();
bfs(graph, dirt, ‘S‘);
}
}

3、本題解法:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> result =new ArrayList<>();
        if(root==null) return result;
        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        queue.add(root);
        
        while (!queue.isEmpty()){
            int cur=queue.size();
            double sum=0;
            for(int i=0;i<cur;i++){
                TreeNode treeNode=queue.poll();
                sum+=treeNode.val;
                if(treeNode.left!=null)
                    queue.add(treeNode.left);
                if(treeNode.right!=null)
                    queue.add(treeNode.right);
            }
            result.add(sum/cur);
        }
        return result;
    }
}

637. Average of Levels in Binary Tree(一棵樹每層節點的平均數)(二叉樹的層序遍歷)