1、
2、递归搞清每一步干什么,什么时候结束
/**
* 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:
vector<string> result;
vector<string> binaryTreePaths(TreeNode* root) {
dfs(root,"");
return result;
}
void dfs(TreeNode* root,string s)
{
if(root == nullptr) return;
string tmp=to_string(root->val);
if(root->left == nullptr && root->right == nullptr)
{
result.push_back(s+tmp);
return;
}
s+=tmp+"->";
dfs(root->left,s);
dfs(root->right,s);
}
};