问题遇到的现象和发生背景
https://leetcode-cn.com/problems/smallest-string-starting-from-leaf/
问题相关代码,请勿粘贴截图
class Solution {
vector<string> result;
void dfs(TreeNode* root,string path){
if(root == nullptr)
return;
path+='a'+root->val;
if(root->left == nullptr&&root->right == nullptr){
reverse(path.begin(),path.end());
result.push_back(path);
return;
}
dfs(root->left,path);
dfs(root->right,path);
}
public:
string smallestFromLeaf(TreeNode* root) {
dfs(root,"");
sort(result.begin(),result.end());
return result[0];
}
};
我的解答思路和尝试过的方法
我想要达到的结果