leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0988.cpp (707B)


      1 class Solution {
      2   public:
      3     string smallestFromLeaf(const TreeNode *root) const {
      4         queue<pair<const TreeNode *, string>> q;
      5         q.emplace(root, "");
      6 
      7         string res(16, 'z');
      8         while (!q.empty()) {
      9             for (int k = q.size(); k > 0; k--) {
     10                 auto [root, s] = q.front();
     11                 q.pop();
     12                 s += 'a' + root->val;
     13 
     14                 if (root->left) q.emplace(root->left, s);
     15                 if (root->right) q.emplace(root->right, s);
     16 
     17                 if (!root->left && !root->right) {
     18                     reverse(begin(s), end(s));
     19                     res = min(res, s);
     20                 }
     21             }
     22         }
     23 
     24         return res;
     25     }
     26 };