“Range Sum of BST” Javascript solution Cheat Sheet

The Range Sum of the BST challenge is in the easy category and is a good start for understanding more challenging algorithms using trees as a data structure. I will focus on explaining a solution that works and not on the O time and space complexity.

Challenge: “Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].”LeetCode




1. Sum result will be pushed to an array

sum = [];

2. Create a helper method for the node

function dsf(node){

3. If no node return null

if(!node) return null;

4. Starting by left branch then right



5. If the node value is greater or equal to low and smaller or equal to high it is within the range

if (node.val >= low && node.val <= high){

6. If within the range, push node value to the sum



7. Return sum by addition nodes values using reduce

return sum.reduce((a,b)=> a+b,0 )

Solution :



Lifelong learner , Full Stack Software Engineer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store