leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0988.cpp (707B)
0 class Solution { 1 public: 2 string smallestFromLeaf(const TreeNode *root) const { 3 queue<pair<const TreeNode *, string>> q; 4 q.emplace(root, ""); 5 6 string res(16, 'z'); 7 while (!q.empty()) { 8 for (int k = q.size(); k > 0; k--) { 9 auto [root, s] = q.front(); 10 q.pop(); 11 s += 'a' + root->val; 12 13 if (root->left) q.emplace(root->left, s); 14 if (root->right) q.emplace(root->right, s); 15 16 if (!root->left && !root->right) { 17 reverse(begin(s), end(s)); 18 res = min(res, s); 19 } 20 } 21 } 22 23 return res; 24 } 25 };