Loading...
Loading...
RangeError: Maximum call stack size exceededThis error means a function is calling itself recursively without a proper base case, causing the call stack to overflow. Each function call adds a frame to the stack — when it gets too deep, Node.js throws this error.
Every recursive function must have a condition that stops the recursion.
// ❌ No base case — infinite recursion
function countdown(n) {
console.log(n);
countdown(n - 1); // never stops!
}
// ✅ With base case
function countdown(n) {
if (n <= 0) return; // base case
console.log(n);
countdown(n - 1);
}For very deep trees, use an iterative approach with a stack.
// ✅ Iterative tree traversal (no stack overflow)
function traverseTree(root) {
const stack = [root];
while (stack.length > 0) {
const node = stack.pop();
console.log(node.value);
if (node.right) stack.push(node.right);
if (node.left) stack.push(node.left);
}
}