Created
April 9, 2017 15:23
-
-
Save s4553711/78ab69a89da5b6de5f4a34ec6aadec26 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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