Created
December 15, 2018 01:23
-
-
Save overcyn/3b904300625dd7e9c5f67a591354bd91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function findMinFromTree(node) { | |
| let minValue = node.value | |
| let minArray = [minValue] | |
| -------------------------------------------------------------- | |
| if (node.left) { | |
| minArray.concat(findMinFromTree(node.left)) | |
| } | |
| if (node.right) { | |
| minArray.concat(findMinFromTree(node.right)) | |
| } | |
| let sorted = minArray.sort(a, b => a - b) | |
| return sorted.slice(0, 2) | |
| -------------------------------------------------------------- | |
| if (node.left) { | |
| let leftMin = findMinFromTree(node.left) | |
| for (let x = 0; x < leftMin.length; x++) { | |
| if (leftMin[x] < minArray[0]) minArray.unshift(leftMin[x]) | |
| else if (leftMin[x] < minArray[1]) minArray[1] = leftMin[x] | |
| else if (!minArray[1]) minArray[1] = leftMin[x] | |
| } | |
| } | |
| if (node.right) { | |
| let rightMin = findMinFromTree(node.left) | |
| for (let x = 0; x < rightMin.length; x++) { | |
| if (rightMin[x] < minArray[0]) minArray.unshift(rightMin[x]) | |
| else if (rightMin[x] < minArray[1]) minArray[1] = rightMin[x] | |
| else if (!minArray[1]) minArray[1] = rightMin[x] | |
| } | |
| } | |
| return minArray | |
| } | |
| function findSecondSmallest(node) { | |
| let minArray = findMinFromTree(node) | |
| return minArray[1] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment