Workspace Largest Bst Subtree
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Largest BST Subtree visualizer

Learn Largest BST Subtree with a step-by-step Binary Search Trees visualizer, JavaScript source code and a worked example. Practice ordering and operations.

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
[
  10,
  5,
  15,
  1,
  8,
  null,
  7
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let best=0;
function f(i){
if(!exists(a,i))return [true,0,Infinity,-Infinity];
let [l,ls,lmin,lmax]=f(2*i+1),[r,rs,rmin,rmax]=f(2*i+2),v=a[i],ok=l&&r&&lmax<v&&v<rmin,size=ls+rs+1;
if(ok)best=Math.max(best,size);
s("Merge subtree validity and bounds",{
Tree:nodes(a),Current:[i],Valid:[ok],Size:[size],Best:[best]
});
return [ok,size,Math.min(lmin,v),Math.max(rmax,v)]
}
f(0);
return best
};
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

Merge subtree validity and bounds

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

Tree6 rows
10515187Select a node to inspect its connections.
Current1 items
Valid1 items
Size1 items
Best1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Binary Search Trees · Ordering and operations

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

[
  10,
  5,
  15,
  1,
  8,
  null,
  7
]

Execution steps

  1. Merge subtree validity and bounds

Computed output

3
JavaScript reference implementation
const solve = (a,s)=>{
  let best=0;
  function f(i){
    if(!exists(a,i))return [true,0,Infinity,-Infinity];
    let [l,ls,lmin,lmax]=f(2*i+1),[r,rs,rmin,rmax]=f(2*i+2),v=a[i],ok=l&&r&&lmax<v&&v<rmin,size=ls+rs+1;
    if(ok)best=Math.max(best,size);
    s("Merge subtree validity and bounds",{
      Tree:nodes(a),Current:[i],Valid:[ok],Size:[size],Best:[best]
    });
    return [ok,size,Math.min(lmin,v),Math.max(rmax,v)]
  }
  f(0);
  return best
};
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.