Workspace Zigzag Level Order Traversal
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Zigzag Level Order Traversal visualizer

Learn Zigzag Level Order Traversal with a step-by-step Binary Trees visualizer, JavaScript source code and a worked example. Practice traversals.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 7Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": [
    1,
    2,
    3,
    4,
    5,
    null,
    6
  ],
  "mode": "zig"
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,mode
},s)=>{
let q=[[0,0,0]],levels=[],cols=new Map;
while(q.length){
let [i,d,c]=q.shift();
if(!levels[d])levels[d]=[];
levels[d].push(a[i]);
if(!cols.has(c))cols.set(c,[]);
cols.get(c).push([d,a[i]]);
for(let [j,x] of [[2*i+1,c-1],[2*i+2,c+1]])if(exists(a,j))q.push([j,d+1,x]);
s("Visit breadth-first node",{
Tree:nodes(a),Queue:q,Levels:levels,Columns:[...cols].map(([c,v])=>[c,...v.map(x=>x[1])]),Current:[i]
})
}
if(mode==="level")return levels;
if(mode==="zig")return levels.map((r,i)=>i%2?r.reverse():r);
if(mode==="right")return levels.map(r=>r.at(-1));
let v=[...cols].sort((a,b)=>a[0]-b[0]).map(x=>x[1]);
return mode==="top"?v.map(x=>x[0][1]):mode==="bottom"?v.map(x=>x.at(-1)[1]):v.map(x=>x.sort((a,b)=>a[0]-b[0]||a[1]-b[1]).map(y=>y[1]))
};
function exists(a,i){
return i<a.length&&a[i]!=null
}
function nodes(a){
return a.map((v,i)=>v==null?null:[i,v,exists(a,2*i+1)?2*i+1:null,exists(a,2*i+2)?2*i+2:null]).filter(Boolean)
}
function ordered(a,s,mode){
let o=[];
function f(i){
if(!exists(a,i))return;
if(mode==="pre"){
o.push(a[i]);
s("Visit node before children",{
Tree:nodes(a),Visited:o,Current:[i]
})
}
f(2*i+1);
if(mode==="in"){
o.push(a[i]);
s("Visit node between children",{
Tree:nodes(a),Visited:o,Current:[i]
})
}
f(2*i+2);
if(mode==="post"){
o.push(a[i]);
s("Visit node after children",{
Tree:nodes(a),Visited:o,Current:[i]
})
}
}
f(0);
return o
}

Execution canvas

Active Changed
STEP 1

Visit breadth-first node

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Tree6 rows
123456Select a node to inspect its connections.
Queue2 rows
Levels1 rows
Columns1 rows
Current1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Binary Trees · Traversals

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

{
  "a": [
    1,
    2,
    3,
    4,
    5,
    null,
    6
  ],
  "mode": "zig"
}

Execution steps

  1. Visit breadth-first node

Computed output

[
  [
    1
  ],
  [
    3,
    2
  ],
  [
    4,
    5,
    6
  ]
]
JavaScript reference implementation
const solve = ({
  a,mode
},s)=>{
  let q=[[0,0,0]],levels=[],cols=new Map;
  while(q.length){
    let [i,d,c]=q.shift();
    if(!levels[d])levels[d]=[];
    levels[d].push(a[i]);
    if(!cols.has(c))cols.set(c,[]);
    cols.get(c).push([d,a[i]]);
    for(let [j,x] of [[2*i+1,c-1],[2*i+2,c+1]])if(exists(a,j))q.push([j,d+1,x]);
    s("Visit breadth-first node",{
      Tree:nodes(a),Queue:q,Levels:levels,Columns:[...cols].map(([c,v])=>[c,...v.map(x=>x[1])]),Current:[i]
    })
  }
  if(mode==="level")return levels;
  if(mode==="zig")return levels.map((r,i)=>i%2?r.reverse():r);
  if(mode==="right")return levels.map(r=>r.at(-1));
  let v=[...cols].sort((a,b)=>a[0]-b[0]).map(x=>x[1]);
  return mode==="top"?v.map(x=>x[0][1]):mode==="bottom"?v.map(x=>x.at(-1)[1]):v.map(x=>x.sort((a,b)=>a[0]-b[0]||a[1]-b[1]).map(y=>y[1]))
};
function exists(a,i){
  return i<a.length&&a[i]!=null
}
function nodes(a){
  return a.map((v,i)=>v==null?null:[i,v,exists(a,2*i+1)?2*i+1:null,exists(a,2*i+2)?2*i+2:null]).filter(Boolean)
}
function ordered(a,s,mode){
  let o=[];
  function f(i){
    if(!exists(a,i))return;
    if(mode==="pre"){
      o.push(a[i]);
      s("Visit node before children",{
        Tree:nodes(a),Visited:o,Current:[i]
      })
    }
    f(2*i+1);
    if(mode==="in"){
      o.push(a[i]);
      s("Visit node between children",{
        Tree:nodes(a),Visited:o,Current:[i]
      })
    }
    f(2*i+2);
    if(mode==="post"){
      o.push(a[i]);
      s("Visit node after children",{
        Tree:nodes(a),Visited:o,Current:[i]
      })
    }
  }
  f(0);
  return o
}

Continue learning

Practice related DSA questions in the same topic.