二叉树的层序遍历
function BFS(root) { const queue = [] // 初始化队列queue // 根结点首先入队 queue.push(root) // 队列不为空,说明没有遍历完全 while(queue.length) { const top = queue[0] // 取出队头元素 // 访问 top console.log(top.val) // 如果左子树存在,左子树入队 if(top.left) { queue.push(top.left) } // 如果右子树存在,右子树入队 if(top.right) { queue.push(top.right) } queue.shift() // 访问完毕,队头元素出队 } } -> ABCDEF