leetcode

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

commit 860f13b55e962740947002ba2096fc8dd500c330
parent f7408deb1ef4deaea2cade8be3ef00adcf25479c
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 16 Jul 2024 23:47:57 +0200

Daily Problem

Diffstat:
AProblems/2096.cpp | 24++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/Problems/2096.cpp b/Problems/2096.cpp @@ -0,0 +1,24 @@ +class Solution { + bool find(TreeNode *n, int val, string &path) const { + if (n->val == val) return true; + + if (n->left && find(n->left, val, path)) + path.push_back('L'); + else if (n->right && find(n->right, val, path)) + path.push_back('R'); + + return !path.empty(); + } + + public: + string getDirections(TreeNode *root, int startValue, int destValue) const { + string s, d; + + find(root, startValue, s); + find(root, destValue, d); + + while (!s.empty() && !d.empty() && s.back() == d.back()) + s.pop_back(), d.pop_back(); + return string(s.size(), 'U') + string(rbegin(d), rend(d)); + } +}; diff --git a/README.md b/README.md @@ -1061,6 +1061,7 @@ for solving problems. | 2091 | Medium | [Removing Minimum and Maximum From Array](Problems/2091.cpp) | | 2092 | Hard | [Find All People With Secret](Problems/2092.cpp) | | 2095 | Medium | [Delete the Middle Node of a Linked List](Problems/2095.cpp) | +| 2096 | Medium | [Step-By-Step Directions From a Binary Tree Node to Another](Problems/2096.cpp) | | 2101 | Medium | [Detonate the Maximum Bombs](Problems/2101.cpp) | | 2104 | Medium | [Sum of Subarray Ranges](Problems/2104.cpp) | | 2108 | Easy | [Find First Palindromic String in the Array](Problems/2108.cpp) |