题目链接:
https://www.nowcoder.com/practice/04a5560e43e24e9db4595865dc9c63a3
解法分析:纯的层序遍历,使用 BFS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
   | 
 
 
 
 
 
 
 
 
 
 
 
  function levelOrder(root) {          if (!root) {         return [];     }     const res = [], queue = [];     queue.push(root);
           while (queue.length) {         const currentLevelSize = queue.length;         res.push([]);                  for (let i = 0; i < currentLevelSize; i++) {             const node = queue.shift();             res[res.length - 1].push(node.val);
                           node.left && queue.push(node.left);             node.right && queue.push(node.right);         }     }          return res; }
  module.exports = {     levelOrder : levelOrder };
 
  |