no global leaves var

master
Matthew Huntington 3 years ago
parent c1c4ef0f8e
commit 177a450d1b

@ -43,20 +43,21 @@ const tree = breakDown({value:5});
console.dir(tree, {depth:null}); console.dir(tree, {depth:null});
const getLeaves = (node, leafArray)=>{ const getLeaves = (node)=>{
if(node.twoPath === undefined){ if(node.twoPath === undefined){
return node.value; return node.value;
} }
const leftValue = getLeaves(node.twoPath.left, leafArray) const twoLeftValue = getLeaves(node.twoPath.left);
const rightValue = getLeaves(node.twoPath.right, leafArray) const twoRightValue = getLeaves(node.twoPath.right);
leafArray.push([leftValue, rightValue]);
if(node.threePath !== undefined){ if(node.threePath !== undefined){
const leftValue = getLeaves(node.threePath.left, leafArray) const threeLeftValue = getLeaves(node.twoPath.left);
const rightValue = getLeaves(node.threePath.right, leafArray) const threeRightValue = getLeaves(node.twoPath.right);
leafArray.push([leftValue, rightValue]); return [[twoLeftValue, twoRightValue], [threeLeftValue, threeRightValue]];
} }
return [twoLeftValue, twoRightValue];
} }
const leaves = [] const leaves = getLeaves(tree);
getLeaves(tree, leaves);
console.log(leaves); console.log(leaves);

Loading…
Cancel
Save