Skip to content

Instantly share code, notes, and snippets.

@xzhang311
Created June 29, 2016 15:33
/**
* 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:
void helper(vector<string>& res, string tmpRes, TreeNode* root){
if(root==NULL)
return;
tmpRes = tmpRes.length()==0? to_string(root->val):tmpRes+string("->")+to_string(root->val);
if(root->left==NULL&&root->right==NULL){
res.push_back(tmpRes);
return;
}
helper(res, tmpRes, root->left);
helper(res, tmpRes, root->right);
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
string tmpRes;
helper(res, tmpRes, root);
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment