fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8. int data;
  9. Node *left;
  10. Node *right;
  11. Node(int val)
  12. {
  13. data = val;
  14. left = right = NULL;
  15. }
  16. };
  17.  
  18. Node *build()
  19. {
  20. Node *root = new Node(50);
  21. root->left = new Node(45);
  22. root->right = new Node(60);
  23.  
  24. cout << "In Build Function\n";
  25.  
  26. cout << root << " " << root->data << endl;
  27. cout << root->left << " " << root->left->data << endl;
  28. cout << root->right << " " << root->right->data << endl;
  29.  
  30. return root;
  31. }
  32.  
  33. void inorder(Node *root, vector<Node *> &A)
  34. {
  35. if (root == NULL)
  36. return;
  37.  
  38. inorder((root)->left, A);
  39. // free(root->left);
  40.  
  41. A.push_back(root);
  42.  
  43. cout << root << " " << root->data << endl;
  44.  
  45. inorder(root->right, A);
  46. // free(root->right);
  47. }
  48.  
  49. void print(vector<Node *> &A)
  50. {
  51. cout << "\nprint in function\n";
  52. for (int i = 0; i < A.size(); i++)
  53. cout << A[i] << " " << A[i]->data << endl;
  54. cout << endl;
  55. }
  56.  
  57. int main()
  58. {
  59. Node *root = build();
  60.  
  61. cout << "\nIn Main Function\n";
  62.  
  63. cout << root << " " << root->data << endl;
  64. cout << root->left << " " << root->left->data << endl;
  65. cout << root->right << " " << root->right->data << endl;
  66.  
  67. vector<Node *> A;
  68. cout << "\nIn inorder Function\n";
  69. inorder(root, A);
  70.  
  71. print(A);
  72. cout << "\nIn Main Function\n";
  73.  
  74. cout << root << " " << root->data << endl;
  75. cout << root->left << " " << root->left->data << endl;
  76. cout << root->right << " " << root->right->data << endl;
  77. }
  78.  
Success #stdin #stdout 0s 4280KB
stdin
Standard input is empty
stdout
In Build Function
0x56330bb43e70 50
0x56330bb43e90 45
0x56330bb43eb0 60

In Main Function
0x56330bb43e70 50
0x56330bb43e90 45
0x56330bb43eb0 60

In inorder Function
0x56330bb43e90 45
0x56330bb43e70 50
0x56330bb43eb0 60

print in function
0x56330bb43e90   45
0x56330bb43e70   50
0x56330bb43eb0   60


In Main Function
0x56330bb43e70 50
0x56330bb43e90 45
0x56330bb43eb0 60