1
var smallestFromLeaf = function (root) {2
if (root === null) return "";4
let queue = [[root, "" + giveCharacter(root.val)]];5
let leafLevelFound = false;6
let possibleSmallString = [];8
while (queue.length > 0) {9
let currentLevelLength = queue.length;11
for (let i = 0; i < currentLevelLength; i++) {12
let [currentNode, currentPath] = queue.shift();14
if (currentNode.left === null && currentNode.right === null) {15
// as one of the test case is failing with this approacch - saying legth/depth of the path doesnt matter16
// even TOTAL (ASCII)SUM of letters also not matter - it should be dictionary first path17
// hence, no need of this logic and have to continue until all path discovered18
// So, instead removing - just never doing TRUE - hence it will continue exploring and putting all paths19
leafLevelFound = false;20
possibleSmallString.push(currentPath); //.split("").reverse().join("")23
if (!leafLevelFound) {24
if (currentNode.left !== null)27
giveCharacter(currentNode.left.val) + currentPath,29
if (currentNode.right !== null)32
giveCharacter(currentNode.right.val) + currentPath,37
if (leafLevelFound) break;40
// console.log(possibleSmallString);41
possibleSmallString.sort();42
// console.log(possibleSmallString);43
return possibleSmallString[0];46
function giveCharacter(num) {47
return String.fromCharCode(num + 97);