Likou brush question notes day9 (print binary tree from top to bottom)

The first type (print from left to right)

Print each node of the binary tree from top to bottom, and the nodes at the same level are printed from left to right.

topic

insert image description here

train of thought

Print in the order of the same layer from left to right, so use breadth-first traversal, first traverse the same layer, and then traverse the next layer. To implement the breadth-first traversal strategy of the tree, it is necessary to use the first-in-first-out feature of the queue, otherwise it will still be made depth-first every time. The nature of the left and right nodes of the tree determines that the left node will be traversed first every time.

the code

var levelOrder = function(root) {
    
    
    let res = [];
    if(!root) {
    
    
        return res;
    }
    // 初始化队列
    let queue = [root]
    // 跳出条件,队列为空,即所有节点都遍历完成
    while(queue.length) {
    
    
        // 拿出队首节点
        let node = queue.shift();
        res.push(node.val);
        if(node.left) {
    
    
            queue.push(node.left)
        }
        if(node.right) {
    
    
            queue.push(node.right)
        }
    }
    return res
};

The second (print each layer to one line)

Print the binary tree layer by layer from top to bottom, the nodes of the same layer are printed in order from left to right, and each layer is printed to one line.

topic

insert image description here

train of thought

Compared with the first type, the final output of the second type is a similar two-dimensional array, so a double loop is definitely required. In the second loop, a variable needs to be defined to store the data of one layer.

the code

var levelOrder = function(root) {
    
    
    let res =[]
    if(!root){
    
    
        return res
    }
    let queue = [];
    queue.push(root)
    while(queue.length>0){
    
    
        let len=queue.length;
        let tempArr = []
        for(let i =0; i<len;i++){
    
    
            let nodes = queue.shift()
            tempArr.push(nodes.val);
            if(nodes.left){
    
    
                queue.push(nodes.left)
            }
            if(nodes.right){
    
    
                queue.push(nodes.right)
            }
        }
        res.push(tempArr);
    }
    return res
};

The third type (first layer from left to right, second layer from right to left)

Please implement a function to print the binary tree in zigzag order, that is, the first line is printed from left to right, the second layer is printed from right to left, the third line is printed from left to right, and the others and so on.

topic

insert image description here

train of thought

Compared with the second method, it is to reverse the order of the double-layer level time array.

the code

var levelOrder = function(root) {
    
    
  if (!root) return [];
    let queue = [root];
    let res = [];
    let level = 0; // 代表当前层数
    while (queue.length) {
    
    
        res[level] = []; // 第level层的遍历结果
        let levelNum = queue.length; // 第level层的节点数量
        while (levelNum--) {
    
    
            const front = queue.shift();
            res[level].push(front.val);
            if (front.left) queue.push(front.left);
            if (front.right) queue.push(front.right);
        }
        // 行号是偶数时,翻转当前层的遍历结果
        if (level%2) {
    
    
            res[level].reverse();
        }

        level++;
    }
    return res;
};

Guess you like

Origin blog.csdn.net/weixin_51610980/article/details/128431120