fork download
  1. void leafNodes(Node *root,vector<int> &list){
  2. if(root == NULL){
  3. return ;
  4. }
  5. if(!root->left and !root->right){
  6. list.push_back(root->data);
  7. return;
  8. }
  9. leafNodes(root->left,list);
  10. leafNodes(root->right,list);
  11. }
  12.  
  13.  
  14.  
  15. vector <int> printBoundary(Node *root)
  16. {
  17. vector<int> v;
  18. if (root==NULL || (!root->right and !root->left)) {
  19. if(root==NULL){
  20. return v;
  21. }
  22. else{
  23. v.push_back(root->data);
  24. return v;
  25. }
  26. }
  27.  
  28. // If there is only 1 node print it
  29. // and return
  30. // if (!(root->left) && !(root->right)) {
  31. // cout << root->data << endl;
  32. // return;
  33. // }
  34.  
  35. // List to store order of traversed
  36. // nodes
  37. vector<int> list;
  38. list.push_back(root->data);
  39.  
  40. // Traverse left boundary without root
  41. // and last node
  42. Node* L = root->left;
  43. while (L) {
  44. if(L->left){
  45.  
  46. list.push_back(L->data);
  47. L = L->left;
  48.  
  49. }
  50. else if(L->right){
  51.  
  52. list.push_back(L->data);
  53. L = L->right;
  54.  
  55. }
  56. else {
  57. break;
  58. }
  59. }
  60.  
  61. // BFS designed to only include leaf nodes
  62. // queue<Node*> q;
  63. // q.push(root);
  64. // while (!q.empty()) {
  65. // Node* temp = q.front();
  66. // q.pop();
  67. // if (!(temp->left) && !(temp->right)) {
  68. // list.push_back(temp->data);
  69. // }
  70. // if (temp->left) {
  71. // q.push(temp->left);
  72. // }
  73. // if (temp->right) {
  74. // q.push(temp->right);
  75. // }
  76. // }
  77.  
  78. leafNodes(root,list);
  79.  
  80. // Traverse right boundary without root
  81. // and last node
  82. vector<int> list_r;
  83. Node* R = root->right;
  84. while (R) {
  85. if(R->right){
  86. list_r.push_back(R->data);
  87. R = R->right;
  88. }
  89. else if(R->left){
  90. list_r.push_back(R->data);
  91. R = R->left;
  92. }
  93. else {
  94. break;
  95. }
  96. }
  97.  
  98. // Reversing the order
  99. reverse(list_r.begin(), list_r.end());
  100.  
  101. // Concatenating the two lists
  102. list.insert(list.end(), list_r.begin(),
  103. list_r.end());
  104.  
  105. // // Printing the node's data from the list
  106. // for (auto i : list) {
  107. // cout << i->data << " ";
  108. // }
  109. // cout << endl;
  110. return list;
  111.  
  112. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:16: error: variable or field ‘leafNodes’ declared void
 void leafNodes(Node *root,vector<int> &list){
                ^~~~
prog.cpp:1:16: error: ‘Node’ was not declared in this scope
prog.cpp:1:22: error: ‘root’ was not declared in this scope
 void leafNodes(Node *root,vector<int> &list){
                      ^~~~
prog.cpp:1:22: note: suggested alternative: ‘bool’
 void leafNodes(Node *root,vector<int> &list){
                      ^~~~
                      bool
prog.cpp:1:27: error: ‘vector’ was not declared in this scope
 void leafNodes(Node *root,vector<int> &list){
                           ^~~~~~
prog.cpp:1:34: error: expected primary-expression before ‘int’
 void leafNodes(Node *root,vector<int> &list){
                                  ^~~
prog.cpp:15:1: error: ‘vector’ does not name a type
 vector <int> printBoundary(Node *root)
 ^~~~~~
stdout
Standard output is empty