二叉树层序遍历
利用队列结构
function PrintFromTopToBottom(root) { // write code here if(!root) return [] let queue = []; queue.unshift(root); //输出数列 let res = []; while(queue.length > 0){ let tmp = queue.pop(); res.push(tmp.val); if(tmp.left) queue.unshift(tmp.left); if(tmp.right) queue.unshift(tmp.right); } return res; }