Skip to content

Instantly share code, notes, and snippets.

@lamchau
Created March 30, 2015 05:40
Show Gist options
  • Select an option

  • Save lamchau/f5ab7b6891d0cd0183f0 to your computer and use it in GitHub Desktop.

Select an option

Save lamchau/f5ab7b6891d0cd0183f0 to your computer and use it in GitHub Desktop.
var node = {
value: -120,
left: {
value: -2120,
left: { value: -20 },
right: { value: -30 }
},
right: {
value: -20,
left: {value: 20 },
right: { value: 300 }
}
};
function getMax(x) {
if (typeof x === 'undefined' || x === null) {
return 0;
}
var left = getMax(x.left);
var right = getMax(x.right);
return x.value + Math.max(left, right);
}
console.log(getMax(node));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment