leetcode

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

commit 5a08dc932bb72bf450cf0bc6f9479cae00ad0ed1
parent db651b59c2b61fbd55c10b73ec0722631247b3ed
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 17 Apr 2024 19:23:11 +0200

Daily Problem

Diffstat:
AProblems/0988.cpp | 26++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/Problems/0988.cpp b/Problems/0988.cpp @@ -0,0 +1,26 @@ +class Solution { + public: + string smallestFromLeaf(const TreeNode *root) const { + queue<pair<const TreeNode *, string>> q; + q.emplace(root, ""); + + string res(16, 'z'); + while (!q.empty()) { + for (int k = q.size(); k > 0; k--) { + auto [root, s] = q.front(); + q.pop(); + s += 'a' + root->val; + + if (root->left) q.emplace(root->left, s); + if (root->right) q.emplace(root->right, s); + + if (!root->left && !root->right) { + reverse(begin(s), end(s)); + res = min(res, s); + } + } + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -583,6 +583,7 @@ for solving problems. | 0983 | Medium | [Minimum Cost For Tickets](Problems/0983.cpp) | | 0985 | Medium | [Sum of Even Numbers After Queries](Problems/0985.cpp) | | 0986 | Medium | [Interval List Intersections](Problems/0986.cpp) | +| 0988 | Medium | [Smallest String Starting From Leaf](Problems/0988.cpp) | | 0989 | Easy | [Add to Array-Form of Integer](Problems/0989.cpp) | | 0990 | Medium | [Satisfiability of Equality Equations](Problems/0990.cpp) | | 0991 | Medium | [Broken Calculator](Problems/0991.cpp) |