<p>You are given the <code>root</code> of a binary tree where each node has a value in the range <code>[0, 25]</code> representing the letters <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code>.</p>

<p>Return <em>the <strong>lexicographically smallest</strong> string that starts at a leaf of this tree and ends at the root</em>.</p>

<p>As a reminder, any shorter prefix of a string is <strong>lexicographically smaller</strong>.</p>

<ul>
	<li>For example, <code>&quot;ab&quot;</code> is lexicographically smaller than <code>&quot;aba&quot;</code>.</li>
</ul>

<p>A leaf of a node is a node that has no children.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/01/30/tree1.png" style="width: 534px; height: 358px;" />
<pre>
<strong>Input:</strong> root = [0,1,2,3,4,3,4]
<strong>Output:</strong> &quot;dba&quot;
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/01/30/tree2.png" style="width: 534px; height: 358px;" />
<pre>
<strong>Input:</strong> root = [25,1,3,1,3,0,2]
<strong>Output:</strong> &quot;adz&quot;
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/01/tree3.png" style="height: 490px; width: 468px;" />
<pre>
<strong>Input:</strong> root = [2,2,1,null,1,0,null,0]
<strong>Output:</strong> &quot;abc&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 8500]</code>.</li>
	<li><code>0 &lt;= Node.val &lt;= 25</code></li>
</ul>

/**
 * 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:

    bool isLessThan(string& a, string& b)
    {
        int aSize = a.size();
        int bSize = b.size();
        int minSize = min(aSize, bSize);
        
        for (int i = 0; i < minSize; i++)
        {
            if (a[aSize - i - 1] < b[bSize - i - 1])
            {
                return true;
            }
            else if (b[bSize - i - 1] < a[aSize - i - 1])
            {
                return false;
            }
        }

        return (a.size() < b.size());
    }

    string smallestFromLeafRecursive(TreeNode* root, string& parent) {
        if ((root->left == nullptr) && (root->right == nullptr))
        {
            return parent + std::string(1, static_cast<char>('a' + root->val));
        }

        std::string left;
        std::string right;
        std::string self = std::string(1, static_cast<char>('a' + root->val));
        parent += self;
        
        if (root->left != nullptr)
        {
            left = smallestFromLeafRecursive(root->left, parent);
        }
        if (root->right != nullptr)
        {
            right = smallestFromLeafRecursive(root->right, parent);
        }
        parent.pop_back();

        if (root->left == nullptr)
        {
            return right;
        }

        if (root->right == nullptr)
        {
            return left;
        }
        //std::cout << left << "," << right << "," <<  isLessThan(left, right);
        return isLessThan(left, right) ? left : right;
    }

    void smallestFromLeafRecursive2(TreeNode* root, string& parent, string& ans) {
        std::string self = std::string(1, static_cast<char>('a' + root->val));
        parent += self;
        
        if ((root->left == nullptr) && (root->right == nullptr))
        {
            if (ans.empty() || isLessThan(parent, ans))
            {
                ans = parent;
            }
            parent.pop_back();
            return;
        }

        if (root->left != nullptr)
        {
            smallestFromLeafRecursive2(root->left, parent, ans);
        }
        if (root->right != nullptr)
        {
            smallestFromLeafRecursive2(root->right, parent, ans);
        }
        parent.pop_back();
    }

    string smallestFromLeaf(TreeNode* root) {
        std::string empty;
        string reversed;
        smallestFromLeafRecursive2(root, empty, reversed);
        int size = reversed.size();
        string ans(size, ' ');

        for (int i = 0; i < ans.size(); i++)
        {
            ans[i] = reversed[size - i - 1];
        }
        return ans;
    }
};
19400