The approach for this kind of question would be to traverse the binary tree in level order traversal(breadth first search) and then find the maximum for each level. The level-order traversal needs to be modified so that nodes on each level can be iterated.

In this case, there are two loops in 'findMaxEveryLevel' function. The outer loop keeps track of the number of nodes in the 'queue' using 'nodeCount' variable which represents the number of nodes in the current level. The inner loop iterates over each node in the current level and calculates the maximum for that level. Finally, once the inner loop is complete the outer loop logs the maximum value in the current level of the binary tree and sets the 'max' variable to be 'Number.MIN_VALUE'. This goes on for each level of the binary tree until the last level.

Where n is the total number of nodes in the binary tree.

class Node{
    constructor(val){
        this.val = val;
        this.left = null;
        this.right = null;
    }
}

let findMaxEveryLevel = (root)=>{
    let queue = new Queue();
    let nodeCount = 0;
    let max = Number.MIN_VALUE;
    queue.push(root);

    while(true){

        nodeCount = queue.size(); 

        if(nodeCount === 0){
            break;
        }

        while(nodeCount>0){

            let currentNode = queue.pop();//same as polling a queue

            console.log(currentNode.val);//bfs output

            if(currentNode.left!==null){
                queue.push(currentNode.left);
            }

            if(currentNode.right!==null){
                queue.push(currentNode.right);
            }

            max = Math.max(currentNode.val, max);
            nodeCount--;
        }

        console.log(`Max for current level: ${max} \n`);
        max = Number.MIN_VALUE;
    }
};

This is a queue helper class that we use to guarantee O(1) time complexity for push, pop, peek and empty methods.

class LinkedListNode {
    constructor(val) {
        this.val = val;
        this.next = null;
      }
}
    
class Queue {
    
    constructor(){
        this.head = null;
        this.tail = null;
        this.queueSize = 0;
    }
    
    push(val){
        if(this.tail === null && this.head === null){
            this.head = new LinkedListNode(val);
            this.tail = this.head;
        }else{
            const newNode = new LinkedListNode(val);
            this.tail.next = newNode;
            this.tail = this.tail.next;
        }        
        this.queueSize++;
    }
    
    pop(){
        if(this.head!==null){
            const result = this.head.val;
            this.head = this.head.next;
            if(this.head === null){
                this.tail = this.head;
            }
            this.queueSize--;
            return result;
        }   
        return null;
    }

    size(){
        return this.queueSize;
    }
    
    peek(){
        if(this.head!==null){
            return this.head.val;
        }
        
        return null;
    }
    
    empty(){
        return this.queueSize === 0;
    }
    
}