-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSmallest String Starting From Leaf.js
46 lines (32 loc) · 1.68 KB
/
Smallest String Starting From Leaf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var smallestFromLeaf = function(root) {
if(root === null) return '';
let queue = [[root, ''+giveCharacter(root.val)]];
let leafLevelFound = false;
let possibleSmallString = [];
while(queue.length > 0){
let currentLevelLength = queue.length;
for(let i=0; i<currentLevelLength; i++){
let [currentNode, currentPath] = queue.shift();
if(currentNode.left === null && currentNode.right ===null){
// as one of the test case is failing with this approacch - saying legth/depth of the path doesnt matter
// even TOTAL (ASCII)SUM of letters also not matter - it should be dictionary first path
// hence, no need of this logic and have to continue until all path discovered
// So, instead removing - just never doing TRUE - hence it will continue exploring and putting all paths
leafLevelFound = false;
possibleSmallString.push(currentPath); //.split("").reverse().join("")
}
if(!leafLevelFound){
if(currentNode.left !== null) queue.push([currentNode.left,giveCharacter(currentNode.left.val)+currentPath]);
if(currentNode.right !== null) queue.push([currentNode.right,giveCharacter(currentNode.right.val)+currentPath]);
}
}
if(leafLevelFound) break;
}
// console.log(possibleSmallString);
possibleSmallString.sort();
// console.log(possibleSmallString);
return possibleSmallString[0];
};
function giveCharacter(num){
return String.fromCharCode(num+97);
}