Every type of Recursion Solution is here
Recursion in JavaScript is a technique where a function calls itself to solve a problem. It breaks the problem down into smaller subproblems, typically with a base case to stop the recursion. Each recursive call works on a simpler or smaller instance of the original problem. When the base case is met, the recursion stops, and the results are combined or returned.
const tree = ["apple", {left:"apple"},["apple"], {left:{apple:"apple"}}]
let count = 0;
function countNumberOfApples(tree) {
?if (!tree) return count;??
?if (tree === "apple") {
??count += 1;
?}
??if (typeof(tree) == "object") {
???for (let key in tree){?
???countNumberOfApples(tree[key]);
???}
?return count
?}
?if(Array.isArray(tree)){
?for(let i = 0; i<tree.length; i++ ){
??countNumberOfApples(tree[i][`${key}`]);
}}
console.log("count:", count)
?return count;
}
console.log(countNumberOfApples(tree));?