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);