/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void pathfind(TreeNode* root, vector<string>&ans, string currpath){
if(root==NULL)return;
if(root->left==NULL && root->right==NULL){
currpath+=to_string(root->val);
ans.push_back(currpath);
}
currpath+=to_string(root->val)+"->";
if(root->left!=NULL){
pathfind(root->left, ans, currpath);
}
if(root->right!=NULL){
pathfind(root->right, ans, currpath);
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string>ans;
pathfind(root, ans, "");
return ans;
}
};
0 Comments