Skip to content Skip to sidebar Skip to footer

Javascript Recursion Function Returning Undefined

I'm not even certain what the title of this question should be - I'm not sure what is going wrong at all. I'm writing a function that simply loops through a binary tree. Let us say

Solution 1:

Your recursive call have to return the value to the caller

function searchLeft(node, path) {
    if (typeof node.left == 'undefined') {
        console.log(path);
        return path;
    }
    node = JSON.parse(JSON.stringify(node.left));
    path.push(node.data);
    return searchLeft(node, path); //here return
}

Post a Comment for "Javascript Recursion Function Returning Undefined"