Advertisement
jibha

Untitled

Jan 22nd, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14.  
  15.  
  16.  
  17. vector<string> binaryTreePaths(TreeNode* root) {
  18. if(root==nullptr){
  19. return {};
  20. }
  21.  
  22. if(root->right==nullptr&&root->left==nullptr){
  23. string s;
  24. s+=to_string(root->val);
  25. return s;
  26. }
  27.  
  28. if(root->right==nullptr){
  29. vector<string> v=binaryTreePaths(root->left);
  30. vector<string> ret;
  31. for(string s:v){
  32. ret.push_back(to_string(root->val)+s);
  33. }
  34. return ret;
  35. }
  36. if(root->left==nullptr){
  37. vector<string> v=binaryTreePaths(root->right);
  38. vector<string> ret;
  39. for(string s:v){
  40. ret.push_back(to_string(root->val)+s);
  41. }
  42. return ret;
  43. }
  44. vector<string> v=binaryTreePaths(root->right);
  45. vector<string> ret;
  46. for(string s:v){
  47. ret.push_back(to_string(root->val)+s);
  48. }
  49. v=binaryTreePaths(root->left);
  50. for(string s:v){
  51. ret.push_back(to_string(root->val)+s);
  52. }
  53. return ret;
  54. }
  55. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement