Simple JavaScript solution using DFS


  • 0
    var zigzagLevelOrder = function(root, res = [], level = 0) {
        if (!root) return [];
        res[level] = level % 2 ? [root.val, ...res[level] || []] : [...res[level] || [], root.val];
        zigzagLevelOrder(root.left, res, level + 1);
        zigzagLevelOrder(root.right, res, level + 1);
        return res;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.