Workspace Binary Tree Maximum Path Sum
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Binary Tree Maximum Path Sum visualizer

Learn Binary Tree Maximum Path Sum 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 / 6Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": [
    -10,
    9,
    20,
    null,
    null,
    15,
    7
  ],
  "mode": "sum"
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,mode
},s)=>{
let best=mode==="sum"?-Infinity:0,ok=true;
function f(i){
if(!exists(a,i))return 0;
let l=f(2*i+1),r=f(2*i+2),v;
if(mode==="sum"){
l=Math.max(0,l);
r=Math.max(0,r);
best=Math.max(best,a[i]+l+r);
v=a[i]+Math.max(l,r)
}else{
ok=ok&&Math.abs(l-r)<=1;
best=Math.max(best,l+r);
v=1+Math.max(l,r)
}
s("Combine child results",{
Tree:nodes(a),Current:[i],LeftRight:[l,r],Return:[v],Best:[best]
});
return v
}
f(0);
return mode==="balanced"?ok: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

Combine child results

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

Tree5 rows
-10920157Select a node to inspect its connections.
Current1 items
LeftRight2 items
Return1 items
Best1 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

{
  "a": [
    -10,
    9,
    20,
    null,
    null,
    15,
    7
  ],
  "mode": "sum"
}

Execution steps

  1. Combine child results

Computed output

42
JavaScript reference implementation
const solve = ({
  a,mode
},s)=>{
  let best=mode==="sum"?-Infinity:0,ok=true;
  function f(i){
    if(!exists(a,i))return 0;
    let l=f(2*i+1),r=f(2*i+2),v;
    if(mode==="sum"){
      l=Math.max(0,l);
      r=Math.max(0,r);
      best=Math.max(best,a[i]+l+r);
      v=a[i]+Math.max(l,r)
    }else{
      ok=ok&&Math.abs(l-r)<=1;
      best=Math.max(best,l+r);
      v=1+Math.max(l,r)
    }
    s("Combine child results",{
      Tree:nodes(a),Current:[i],LeftRight:[l,r],Return:[v],Best:[best]
    });
    return v
  }
  f(0);
  return mode==="balanced"?ok: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.