Workspace Count Complete Tree Nodes
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Count Complete Tree Nodes visualizer

Learn Count Complete Tree Nodes with a step-by-step Binary Trees visualizer, JavaScript source code and a worked example. Practice structure and paths.

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 / 5Execution step
Reference example · JavaScript
Inspect reference input
[
  1,
  2,
  3,
  4,
  5,
  6
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
function f(i){
if(!exists(a,i))return 0;
let l=0,r=0,j=i;
while(exists(a,j)){
l++;
j=2*j+1
}
j=i;
while(exists(a,j)){
r++;
j=2*j+2
}
s("Compare extreme heights",{
Tree:nodes(a),Current:[i],Heights:[l,r]
});
return l===r?2**l-1:1+f(2*i+1)+f(2*i+2)
}
return f(0)
};
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

Compare extreme heights

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.
Current1 items
Heights2 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Binary Trees · Structure and paths

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

[
  1,
  2,
  3,
  4,
  5,
  6
]

Execution steps

  1. Compare extreme heights

Computed output

6
JavaScript reference implementation
const solve = (a,s)=>{
  function f(i){
    if(!exists(a,i))return 0;
    let l=0,r=0,j=i;
    while(exists(a,j)){
      l++;
      j=2*j+1
    }
    j=i;
    while(exists(a,j)){
      r++;
      j=2*j+2
    }
    s("Compare extreme heights",{
      Tree:nodes(a),Current:[i],Heights:[l,r]
    });
    return l===r?2**l-1:1+f(2*i+1)+f(2*i+2)
  }
  return f(0)
};
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.