题目链接:https://leetcode.com/submissions/detail/134406356/
/**
* 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 {
//1 find the leaf and bush back a string to a vector<string>
public: void helper(TreeNode* root,vector<string>& Str, string s ){
//2,break point
if(!root->left && !root->right){
Str.push_back(s);
return;
}
//3 merge
if(root->left) helper(root->left,Str,s+"->"+to_string(root->left->val));
if(root->right) helper(root->right,Str,s+"->"+to_string(root->right->val));
}
public: vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res ;
if(!root) return res;
helper(root,res, to_string(root->val) );
return res;
}