# LeetCode: branch-sums

Table of Contents

Statement

BST Representation

tree
tree = 10
/ \
5 15
/ \ / \
2 5 13 22
/ \
1 14

Solution Explanation

Before diving into the solution, let’s first understand an important concept to be able to solve this problem.

What is a recursive function?

In simple terms, recursion happens when a function calls itself, but it must have an exit condition to avoid infinite loops. Another concept we need to understand is the call stack, which is important because it helps us trace how recursion works.

So, let’s try an example to make this concept easier to understand. Imagine a stack of bumper plates, like in the image below:

bumper plates
bumper plates

Each plate represents a function call. But if you think about it, you can’t remove plate number five right away — before taking it off, you must first remove all the plates that are on top of it.

That’s how the call stack works. One of the interesting things about this process is that each function keeps its own state saved until the child function finishes and returns control to the parent function.

Each time you go deeper, you’re stacking plates (adding function calls). Each time a function returns, you take a plate off — and everything (variables, runningSum) returns to the exact same state it was in before that call.

So, when recursion “goes back,” it’s not restarting — it’s resuming the previous function with all its data intact, just like your barbell’s weight returns to the earlier total.

Soluction

tree
// Define the BST (Binary Search Tree) class
class BST {
constructor(value) {
this.value = value
this.left = null
this.right = null
}
}
// Main function that starts the process
function branchSums(root) {
const sums = []
// We start recursion from the root, with an initial runningSum of 0
calculateBranchSums(root, 0, sums)
return sums
}
// Recursive helper function
function calculateBranchSums(node, runningSum, sums) {
// 🧱 BASE CASE:
// If the node is null, there's nothing to process, so we stop.
if (node == null) return
// 🏋️‍♂️ Imagine adding a new bumper plate to the bar:
// Each recursive call represents one more function on the call stack.
// We add the current node's value to our running total.
var newrunningSum = runningSum + node.value
// 🌿 If the current node has no children (it's a leaf),
// we’ve reached the bottom of the stack — the end of the branch.
if (node.left == null && node.right == null) {
// Save the total sum of this branch
sums.push(newrunningSum)
// 🧩 Now we return: the function "goes back" to the previous one,
// like removing the top plate from the stack.
return
}
// 🧠 Recursive calls:
// First, we go left → deeper in the branch.
// This means adding another plate (function call) to the stack.
calculateBranchSums(node.left, newrunningSum, sums)
// Then, we go right → another new path to explore.
// Again, we add another plate and go deeper.
calculateBranchSums(node.right, newrunningSum, sums)
// 🌀 After both recursive calls finish,
// this function also returns, removing its "plate" from the stack
// and resuming execution in its parent function.
}
// --- Create the BST structure ---
const root = new BST(10)
root.left = new BST(5)
root.right = new BST(15)
root.left.left = new BST(2)
root.left.right = new BST(5)
root.left.left.left = new BST(1)
root.right.left = new BST(13)
root.right.right = new BST(22)
root.right.left.right = new BST(14)
// --- Run the function ---
console.log(branchSums(root)) // Output: [18, 20, 52, 47]

💡 SUMMARY

Each recursive call adds a new “plate” (function) to the call stack. When a leaf node is reached, recursion “goes back” — removing plates one by one while keeping each function’s own state. In the end, all branches are explored and their sums are collected.

Next: Evaluate Expression Tree
My avatar

Appreciate you reading. If you want more hacking write-ups, network labs, and code deep-dives, check out my other posts or connect via the social links in the footer.


LeetCode Series