Description
Submission
/**
* 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 {
vector<string> ret;
void dfs(TreeNode* root, string s) {
s += "->";
s += to_string(root->val);
if(!root->left && !root->right) {
ret.push_back(s);
return;
}
if(root->left) dfs(root->left, s);
if(root->right) dfs(root->right, s);
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
if(root == NULL) return {};
string s = to_string(root->val);
if(root->left) dfs(root->left, s);
if(root->right) dfs(root->right, s);
if(ret.empty()) ret.push_back(s);
return ret;
}
};