/**
* 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> binaryTreePaths(TreeNode* root) {
vector<string> path;
string cur;
if(root == NULL){
return path;
}
vector<string> left = binaryTreePaths(root->left);
vector<string> right = binaryTreePaths(root->right);
for(string paths:left){
cur = to_string(root->val)+"->"+paths;
path.push_back(cur);
}
for(string paths:right){
cur = to_string(root->val)+"->"+paths;
path.push_back(cur);
}
if(root->left == NULL && root->right == NULL){
cur = to_string(root->val);
path.push_back(cur);
}
return path;
}
};
257. Binary Tree Paths
最新推荐文章于 2020-02-26 21:39:14 发布