text: // Hi, I'm Yanzhan. I'm interested in all kinds of algorithmic problems. Also, I'm fascinated with javascript. If you want to learn more about programming problems and javascript, please visit my Youtube Channel (https://www.youtube.com/channel/UCDkz-__gl3frqLexukpG0DA?view_as=subscriber), Twitter Account (https://twitter.com/YangYanzhan), GitHub Repo (https://github.com/yangyanzhan/code-camp) or HomePage (https://yanzhan.site/).
  
  // 开始分析
  // 1. 层序遍历。
  // 结束分析
  
  // 开始编码
  
  /**
   * Definition for a binary tree node.
   * function TreeNode(val, left, right) {
   *     this.val = (val===undefined ? 0 : val)
   *     this.left = (left===undefined ? null : left)
   *     this.right = (right===undefined ? null : right)
   * }
   */
  
  /**
   * @param {TreeNode} root
   * @return {number[]}
   */
  
  var averageOfLevels = function(root) {
      var res = [];
      if (!root) {
          return res;
      }
      var nodes = [root];
      while (nodes.length > 0) {
          var nextNodes = [], sum = 0, cnt = 0;
          for (var node of nodes) {
              sum += node.val;
              cnt++;
              if (node.left) {
                  nextNodes.push(node.left);
              }
              if (node.right) {
                  nextNodes.push(node.right);
              }
          }
          nodes = nextNodes;
          res.push(sum / cnt);
      }
      return res;
  };
  