Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- vector<string> binaryTreePaths(TreeNode* root) {
- if(root==nullptr){
- return {};
- }
- if(root->right==nullptr&&root->left==nullptr){
- string s;
- s+=to_string(root->val);
- return s;
- }
- if(root->right==nullptr){
- vector<string> v=binaryTreePaths(root->left);
- vector<string> ret;
- for(string s:v){
- ret.push_back(to_string(root->val)+s);
- }
- return ret;
- }
- if(root->left==nullptr){
- vector<string> v=binaryTreePaths(root->right);
- vector<string> ret;
- for(string s:v){
- ret.push_back(to_string(root->val)+s);
- }
- return ret;
- }
- vector<string> v=binaryTreePaths(root->right);
- vector<string> ret;
- for(string s:v){
- ret.push_back(to_string(root->val)+s);
- }
- v=binaryTreePaths(root->left);
- for(string s:v){
- ret.push_back(to_string(root->val)+s);
- }
- return ret;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement