递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void path(string str,TreeNode* root,vector<string>& res){
if(!root->left && !root->right){
res.push_back(str+to_string(root->val));
return;
}
if(root->left){
path(str+to_string(root->val)+"->",root->left,res);
}
if(root->right){
path(str+to_string(root->val)+"->",root->right,res);
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(root)
path("",root,res);
return res;
}
};