103. Zigzag layer sequence traversal of binary tree | Algorithm (leetcode, with mind map + all solutions) 300 questions

Time:2022-11-24

Zero title: algorithm (leetcode, with mind map + all solutions) 300 questions (103) zigzag layer order traversal of binary tree

a topic description

103. Zigzag layer sequence traversal of binary tree | Algorithm (leetcode, with mind map + all solutions) 300 questions
103. Zigzag layer sequence traversal of binary tree | Algorithm (leetcode, with mind map + all solutions) 300 questions

Overview of the second solution (mind map)

103. Zigzag layer sequence traversal of binary tree | Algorithm (leetcode, with mind map + all solutions) 300 questions

All three solutions

1 Scheme 1

1) Code:

// Scheme 1: "Self. Layer order traversal method".

// idea:
// 1) Boundary: If root is null, return [] directly.
// 2) State initialization: q1 = [root], q2 = [], resList = [], level = 0.
// 3) Traverse: The condition is that the queue q1 is not empty.
// 4) Return result: In resList, the element items (array form) with odd subscripts are flipped.
// resList.map((item, level) => level%2 === 1 ? item.reverse() : item);
var zigzagLevelOrder = function(root) {
    // 1) Boundary: If root is null, return [] directly.
    if(root === null){
        return [];
    }

    // 2) State initialization: q1 = [root], q2 = [], resList = [], level = 0.
    let q1 = [root],
        q2 = [],
        resList = [],
        level = 0;
    
    // 3) Traverse: The condition is that the queue q1 is not empty.
    while(q1.length !== 0){
        let temp = q1.shift();
        if(temp.left !== null){
            q2.push(temp.left);
        }
        if(temp.right !== null){
            q2.push(temp.right);
        }
        if(resList[level] === undefined){
            resList[level] = [];
        }
        resList[level].push(temp.val);
        if(q1.length === 0){
            q1 = q2;
            q2 = [];
            level++;
        }
    }

    // 4) Return result: In resList, the element items (array form) with odd subscripts are flipped.
    // resList.map((item, level) => level%2 === 1 ? item.reverse() : item);
    return resList.map((item, level) => level%2 === 1 ? item.reverse() : item);
};

2 Scheme 2

1) Code:

// Option 2 "recursive method (self)".
// Skill: "Generally speaking, recursion is given priority for binary trees, and the formal parameters of recursion can be defined according to the problem."

// idea:
// 1) State initialization: curLevel = 0, curRoot = root, resList = [].
// 2) Call the custom recursive function.
// 3) Return the result resList.
var zigzagLevelOrder = function(root) {
    // recursive implementation
    const dfs = (curLevel = 0, curRoot = null) => {
        // 1) Recursive exit
        if (!curRoot) {
            return;
        }

        // 2) Recursive body
        // 2.1) If the current layer has not been initialized, then resList[curLevel] = [].
        if (!resList[curLevel]) {
            resList[curLevel] = [];
        }

        const {left, right, val} = curRoot;
        // 2.2) If the current level is an odd number, the corresponding array will be "reversed" —— resList[curLevel].unshift(val) .
        if ((curLevel % 2) === 1) {
            resList[curLevel].unshift(val);
        }
        // 2.3) If the current level is even, then the corresponding array will be "inserted" —— resList[curLevel].push(val) .
        else {
            resList[curLevel].push(val);
        }

        // 2.4) Current level + 1 and recursively process the left subtree.
        dfs(curLevel + 1, left);
        // 2.4) Current level + 1 and recursively process the right subtree.
        dfs(curLevel + 1, right);
    }

    // 1) State initialization: curLevel = 0, curRoot = root, resList = [].
    let curLevel = 0,
        curRoot = root,
        resList = [];

    // 2) Call the custom recursive function.
    dfs(curLevel, curRoot);

    // 3) Return the result resList.
    return resList;
};

Four resource sharing & more

1 Historical Article – Overview

103. Zigzag layer sequence traversal of binary tree | Algorithm (leetcode, with mind map + all solutions) 300 questions
103. Zigzag layer sequence traversal of binary tree | Algorithm (leetcode, with mind map + all solutions) 300 questions

103. Zigzag layer sequence traversal of binary tree | Algorithm (leetcode, with mind map + all solutions) 300 questions

2 Blogger profile

code farmer three less, one dedicated to writingMinimalist, but complete problem solution (algorithm) blogger.
focus onMultiple solutions to one question, structured thinking, welcome to brush through LeetCode together~