getLeaves works with 5

master
Matthew Huntington 3 years ago
parent 092fd18b1a
commit c1c4ef0f8e

@ -39,19 +39,24 @@ const breakDown = (node)=>{
return node;
}
const tree = breakDown({value:9});
const tree = breakDown({value:5});
console.dir(tree, {depth:null});
//const getLeaves = (node, leafArray)=>{
//if(node.left === undefined && node.right === undefined){
//leafArray.push(node.value);
//} else {
//getLeaves(node.left, leafArray)
//getLeaves(node.right, leafArray)
//}
//return leafArray;
//}
//const leaves = getLeaves(tree, []);
const getLeaves = (node, leafArray)=>{
if(node.twoPath === undefined){
return node.value;
}
const leftValue = getLeaves(node.twoPath.left, leafArray)
const rightValue = getLeaves(node.twoPath.right, leafArray)
leafArray.push([leftValue, rightValue]);
if(node.threePath !== undefined){
const leftValue = getLeaves(node.threePath.left, leafArray)
const rightValue = getLeaves(node.threePath.right, leafArray)
leafArray.push([leftValue, rightValue]);
}
}
const leaves = []
getLeaves(tree, leaves);
//console.log(leaves);
console.log(leaves);

Loading…
Cancel
Save