카테고리 없음

[못품/릿코드/javascript] 116. Populating Next Right Pointers in Each Node

_서리__ 2023. 4. 2. 09:14

 

const connect = function(root){
    if(!root){
        return null;
    }
    const queue = [root];
    while(queue.length){
        let levelSize = queue.length;
        let buf = [];
        for(let i=0;i<levelSize;i++){
            let current = queue.shift()
            current.next = queue[0]||null
            if(current.left) buf.push(current.left)
            if(current.right) buf.push(current.right)
        }
        queue.push(...buf)
    }
    return root
}