class Solution {
public:
void dfs(TreeNode* root,string path,vector<string>& ans){
if(root){
path+=to_string(root->val);
if(root->left==nullptr&&root->right==nullptr){
ans.push_back(path);
}else{
path+="->";
dfs(root->left,path,ans);
dfs(root->right,path,ans);
}
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> ans;
dfs(root,"",ans);
return ans;
}
};