Skip to content

Commit 9abfd47

Browse files
committed
988. Smallest String Starting From Leaf
1 parent 7b1d644 commit 9abfd47

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Runtime: 8 ms
2+
// Memory Usage: 20 MB
3+
class Solution {
4+
public:
5+
void solve(TreeNode* root, string t, string &res) {
6+
if (!root) return;
7+
t = (char)(root->val + 'a') + t;
8+
solve(root->left, t, res);
9+
solve(root->right, t, res);
10+
if (!root->left && !root->right && t < res) {
11+
res = t;
12+
}
13+
}
14+
15+
string smallestFromLeaf(TreeNode* root) {
16+
string res = "~";
17+
solve(root, "", res);
18+
return res;
19+
}
20+
};

0 commit comments

Comments
 (0)