Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created April 9, 2017 15:23
Show Gist options
  • Save s4553711/78ab69a89da5b6de5f4a34ec6aadec26 to your computer and use it in GitHub Desktop.
Save s4553711/78ab69a89da5b6de5f4a34ec6aadec26 to your computer and use it in GitHub Desktop.
/**
* 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> path;
vector<string> binaryTreePaths(TreeNode* root) {
findPath(root);
return result;
}
void findPath(TreeNode* root) {
if (root == NULL) {
return;
}
path.push_back(to_string(root->val));
if (root->left == NULL && root->right == NULL) {
stringstream ss;
ss << path[0];
for(int i = 1; i < path.size(); i++) {
ss << "->" << path[i];
}
result.push_back(ss.str());
}
findPath(root->left);
findPath(root->right);
path.pop_back();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment