-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path988.cpp
More file actions
30 lines (26 loc) · 722 Bytes
/
Copy path988.cpp
File metadata and controls
30 lines (26 loc) · 722 Bytes
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
class Solution {
public:
vector<string> vec;
string smallestFromLeaf(TreeNode *root) {
string ret;
if (!root)
return ret;
FindString(root, ret);
for (auto &s:vec)
std::reverse(s.begin(), s.end());
ret = vec[0];
for (int i = 1; i < vec.size(); ++i)
ret = min(ret, vec[i]);
return ret;
}
void FindString(TreeNode *node, string &curr) {
curr += ('a' + node->val);
if (!node->left && !node->right)
vec.push_back(curr);
if (node->left)
FindString(node->left, curr);
if (node->right)
FindString(node->right, curr);
curr.pop_back();
}
};