Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1 / \ 2 3 \ 5
All root-to-leaf paths are:
["1->2->5", "1->3"]
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
Recursion(root ,"",result);
return result;
}
void Recursion(TreeNode* root, string item, vector<string>& result){
if (root ==NULL) {
return ;
}
item =item+to_string(root->val);
if (root->left!=NULL) {
Recursion(root->left, item+"->", result);
}
if (root->right!=NULL) {
Recursion(root->right, item+"->", result);
}
if (root->right==NULL&&root->left==NULL) {
result.push_back(item);
}
}
};