fork download
  1. #include <bits/stdc++.h>
  2. #include <iostream>
  3. #include <stl>
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8. int data;
  9. Node *left, *right;
  10. };
  11.  
  12. Node* newNode(int key)
  13. {
  14. Node* node = new Node;
  15. node->data = key;
  16. node->left = node->right = nullptr;
  17.  
  18. return node;
  19. }
  20.  
  21. Node *createTree(int parent[], int n)
  22. {
  23. unordered_map<int, Node*> namaTree;
  24.  
  25. // create n new tree nodes each having value from 0 to n-1
  26. // and store them in a map
  27. for (int i = 0; i < n; i++)
  28. namaTree[i] = newNode(i);
  29.  
  30. // represents root node of binary tree
  31. Node *root = nullptr;
  32.  
  33. // traverse the parent array and build the tree
  34. for (int i = 0; i < n; i++)
  35. {
  36. // if parent is -1, set root to current node having
  37. // value i (stored in map[i])
  38. if (parent[i] == -1)
  39. root = namaTree[i];
  40. else
  41. {
  42. // get parent node for current node
  43. Node *ptr = map[parent[i]];
  44.  
  45. // if parent's left child is filled, map the node to its right child
  46. if (ptr->left)
  47. ptr->right = map[i];
  48.  
  49. // if parent's left child is empty, map the node to it
  50. else
  51. ptr->left = namaTree[i];
  52. }
  53. }
  54.  
  55.  
  56. // return root of the constructed tree
  57. return root;
  58. }
  59.  
  60. void inorder(Node *root)
  61. {
  62. if (root == nullptr)
  63. return;
  64.  
  65. inorder(root->left);
  66. cout << root->data << " ";
  67. inorder(root->right);
  68. }
  69.  
  70.  
  71.  
  72. // main function
  73. int main()
  74. {
  75. int arr[10];
  76. for(int i=0; i <10; i++){
  77. cin >> arr[i];}
  78. int n = sizeof arr / sizeof arr[0];
  79.  
  80. Node *root = createTree(arr, n);
  81.  
  82. inorder(root);
  83.  
  84. return 0;
  85. }
  86.  
Compilation error #stdin compilation error #stdout 0s 15240KB
stdin
8 9 10 7 3 4 5 3
compilation info
prog.cpp:3:15: fatal error: stl: No such file or directory
 #include <stl>
               ^
compilation terminated.
stdout
Standard output is empty