1、题目
https://leetcode-cn.com/problems/binary-tree-paths/
2、题意
题解1:dfs
/**
* 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> res;
void dfs(string path,TreeNode*&root)
{
if(root)
{
path+=to_string(root->val);
if(!root->left&&!root->right)
res.push_back(path);
else
path+="->";
dfs(path,root->left);
dfs(path,root->right);
}
}
vector<string> binaryTreePaths(TreeNode* root) {
dfs("",root);
return res;
}
};