From dcda81a3253ce2023d416a45c81521a73715be69 Mon Sep 17 00:00:00 2001 From: Matthew Huntington Date: Wed, 19 Apr 2023 14:26:21 -0400 Subject: [PATCH] got 7 to almost work. left side too nested --- random.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/random.js b/random.js index a5903fd..07cbaf9 100644 --- a/random.js +++ b/random.js @@ -47,16 +47,38 @@ const getLeaves = (node)=>{ if(node.twoPath === undefined){ return node.value; } - const twoPathLeftValue = getLeaves(node.twoPath.left); - const twoPathRightValue = getLeaves(node.twoPath.right); - + + let twoPathResult = []; + const twoPathLeftValue = getLeaves(node.twoPath.left); //2 + const twoPathRightValue = getLeaves(node.twoPath.right); //[[2,3],[3,2]] + if(Number.isInteger(twoPathRightValue)){ + twoPathResult[0] = twoPathLeftValue; + twoPathResult[1] = twoPathRightValue; + } else { + //[2] [[2,3],[3,2]] + twoPathRightValue[0].unshift(twoPathLeftValue) + twoPathRightValue[1].unshift(twoPathLeftValue); + twoPathResult = twoPathRightValue; + } + + let threePathResult = []; if(node.threePath !== undefined){ const threePathLeftValue = getLeaves(node.threePath.left); const threePathRightValue = getLeaves(node.threePath.right); - return [[twoPathLeftValue, twoPathRightValue], [threePathLeftValue, threePathRightValue]]; + if(Number.isInteger(threePathRightValue)){ + threePathResult[0] = threePathLeftValue; + threePathResult[1] = threePathRightValue; + } else { + threePathResult[0] = threePathLeftValue; + threePathResult = threePathResult.concat(threePathRightValue); + } + } + + if(threePathResult.length === 0){ + return twoPathResult; + } else { + return [twoPathResult, threePathResult] } - - return [twoPathLeftValue, twoPathRightValue]; } const leaves = getLeaves(tree);