-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0988.SmallestStringStartingFromLeaf.cpp
More file actions
58 lines (50 loc) · 1.76 KB
/
0988.SmallestStringStartingFromLeaf.cpp
File metadata and controls
58 lines (50 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 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:
const bool compare(const vector<char>& a, const vector<char>& b) {
const int aSize = a.size(), bSize = b.size();
const int minSize = min(aSize, bSize);
for (int i = 1; i <= minSize; i++)
if (a[aSize - i] != b[bSize - i])
return a[aSize - i] < b[bSize - i];
return aSize < bSize;
}
void traverse(TreeNode* root, vector<char>& currentPath, vector<char>& bestPath) {
if (root == nullptr) return;
// Append to current path.
currentPath.emplace_back(root->val);
if (root->left == nullptr && root->right == nullptr) {
// Handle leaf.
if (bestPath.empty() || compare(currentPath, bestPath))
bestPath = currentPath;
} else {
// Recurse.
traverse(root->left, currentPath, bestPath);
traverse(root->right, currentPath, bestPath);
}
// Remove from current path.
currentPath.pop_back();
}
string smallestFromLeaf(TreeNode* root) {
// Get best path.
vector<char> currentPath, bestPath;
traverse(root, currentPath, bestPath);
// Convert to string.
string output;
output.reserve(bestPath.size());
for (int i = bestPath.size() - 1; i >= 0; i--)
output += 'a' + bestPath[i];
// Return output string.
return output;
}
};