You are given the root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'.

Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root.

As a reminder, any shorter prefix of a string is lexicographically smaller.


	For example, "ab" is lexicographically smaller than "aba".


A leaf of a node is a node that has no children.

&nbsp;
Example 1:

Input: root = [0,1,2,3,4,3,4]
Output: "dba"


Example 2:

Input: root = [25,1,3,1,3,0,2]
Output: "adz"


Example 3:

Input: root = [2,2,1,null,1,0,null,0]
Output: "abc"


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [1, 8500].
	0 &lt;= Node.val &lt;= 25


class Solution:
    res = 'z' * 13           # init max result, tree depth,  12< log2(8000) < 13
    
    def smallestFromLeaf(self, root: TreeNode) -> str:
        
        def helper(node: TreeNode, prev):
            prev = chr(97 + node.val) + prev
            
            if not node.left and not node.right:
                self.res = min(self.res, prev)
                return
            
            if node.left:
                helper(node.left, prev)
            if node.right:
                helper(node.right, prev)
        
        helper(root, "")
        return self.res
class Solution {
    String result = null;
    public String smallestFromLeaf(TreeNode root) {
        build(root, new StringBuilder());
        return result;
    }

    public void build(TreeNode root, StringBuilder str) {
        if (root == null) return;

        StringBuilder sb = new StringBuilder(str).insert(0, String.valueOf(intToChar(root.val)));

        if (root.left == null && root.right == null) { // we are on a leaf node
            result = result == null || sb.toString().compareTo(result) < 0 ? sb.toString() : result;
            return;
        }
        build(root.left, sb); // build left child
        build(root.right, sb); // build right child
    }

    // turns an int (0-25) into a Character ex: 0 -> a, 1 -> b, 2 -> c
    public Character intToChar(int i) {
        return (char) (i + 'a');
    }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    string res;
    void solve(TreeNode* root,string cur){
        if(!root) return;
        cur.push_back((char)('a'+root->val));//converting integer to corresponding characer
        if(!root->left and !root->right){
			//reversing the string since it is computed from root to leaf, but we need viceversa
            reverse(cur.begin(),cur.end());
            if(res=="" or cur<res) res=cur;//updating the result based on lexicographical order
            return;
        }
        solve(root->left,cur);
        solve(root->right,cur);
        return;
    }
    string smallestFromLeaf(TreeNode* root) {
        if(!root) return ""; 
        solve(root,"");
        return res;
    }
};
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);
}
Smallest String Starting From Leaf