Advertisement
HexOffender

Untitled

Feb 21st, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include "pt4.h"
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5. ofstream out;
  6.  
  7. struct Tree
  8. {
  9. Tree* left = nullptr;
  10. Tree* right= nullptr;
  11. int data = 0;
  12. };
  13.  
  14. Tree* Init(int N, int data)
  15. {
  16. Tree* Node=new Tree;
  17. Node->data = data;
  18. if (N == 0) return nullptr;
  19. if (N == 1) return Node;
  20. Node->left = Init(N-1, 1);
  21. Node->right = Init(N-1, -1);
  22. return Node;
  23. }
  24.  
  25. void Walker(Tree* tree, string path, int sum)
  26. {
  27. if (tree->left == nullptr)
  28. {
  29. if (sum==0)
  30. out << path << endl;
  31. return;
  32. }
  33. else
  34. Walker(tree->left, path + "A", sum + 1);
  35. if (tree->right == nullptr)
  36. {
  37. if (sum == 0)
  38. out << path << endl;
  39. return;
  40. }
  41. else
  42. Walker(tree->right, path + "B", sum - 1);
  43. return;
  44. }
  45.  
  46. void Solve()
  47. {
  48. Task("Recur27");
  49. Tree* tree = nullptr;
  50. string filename; int N;
  51. pt >> N >> filename;
  52. out.open(filename);
  53. tree = Init(N+1, 0);
  54. Walker(tree, "C", 0);
  55. out.close();
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement