fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class node
  5. {
  6. public:
  7. int d1,d2;
  8. node* left;
  9. node* right;
  10.  
  11. node(int x, int y)
  12. {
  13. d1 = x;
  14. d2 = y;
  15. left = NULL;
  16. right = NULL;
  17. }
  18. };
  19.  
  20.  
  21.  
  22. node* buildTree()
  23. {
  24. int x,y;
  25. cin >> x;
  26.  
  27. if(x == -1) return NULL;
  28.  
  29. cin >> y;
  30.  
  31. node* root = new node(x,y);
  32.  
  33. root->left = buildTree();
  34. root->right = buildTree();
  35.  
  36. return root;
  37. }
  38.  
  39. void preorder(node* root)
  40. {
  41. if(root == NULL) return;
  42.  
  43.  
  44. cout << root->d1 << " " << root->d2 << endl;
  45. preorder(root->left);
  46. preorder(root->right);
  47. }
  48.  
  49.  
  50.  
  51. void solve()
  52. {
  53. node* root = buildTree();
  54.  
  55. preorder(root);
  56.  
  57. // swap(*root->left,*root->right);
  58. // swap(root->left,root->right);
  59.  
  60.  
  61.  
  62. }
  63. int main() {
  64.  
  65. int t;
  66. t = 1;
  67. // cin >> t;
  68.  
  69. while(t--)
  70. {
  71. solve();
  72. }
  73. return 0;
  74. }
Success #stdin #stdout 0s 4556KB
stdin
1 2 3 4 -1 -1 5 6 -1 -1
stdout
1 2
3 4
5 6