Solution:
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if( root == NULL )
return res;
if( root->left == NULL && root->right == NULL ){
res.push_back( to_string(root->val) );
return res;
}
vector<string> leftS = binaryTreePaths( root->left );
for (int i = 0; i < leftS.size(); i++) {
res.push_back( to_string(root->val) + "->" + leftS[i]);
}
vector<string> rightS = binaryTreePaths( root->right );
for (int j = 0; j < rightS.size(); j++) {
res.push_back( to_string( root->val ) + "->" + rightS[j]);
}
return res;
}
};
总结: 还是要注意递归终止条件的设置。