(queue, total = 0)
| 41 | } |
| 42 | |
| 43 | const bfs = (queue, total = 0) => { |
| 44 | while (queue.length) { |
| 45 | for (let i = (queue.length - 1); 0 <= i; i--) { |
| 46 | let [ root, max ] = queue.shift(); |
| 47 | |
| 48 | const isGood = max <= root.val; |
| 49 | if (isGood) total++; |
| 50 | |
| 51 | max = Math.max(max, root.val); |
| 52 | |
| 53 | if (root.right) queue.push([ root.right, max ]); |
| 54 | if (root.left) queue.push([ root.left, max ]); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return total; |
| 59 | } |