(queue)
| 37 | }; |
| 38 | |
| 39 | const bfs = (queue) => { |
| 40 | while (queue.length) { |
| 41 | for (let i = queue.length - 1; 0 <= i; i--) { |
| 42 | const node = queue.shift(); |
| 43 | const left = node.right; |
| 44 | const right = node.left; |
| 45 | |
| 46 | node.left = left; |
| 47 | node.right = right; |
| 48 | |
| 49 | if (node.left) queue.push(node.left); |
| 50 | if (node.right) queue.push(node.right); |
| 51 | } |
| 52 | } |
| 53 | }; |