fork download
  1. // Following is the Binary Tree node structure
  2. /**************
  3.  class BinaryTreeNode {
  4.  public :
  5.  T data;
  6.  BinaryTreeNode<T> *left;
  7.  BinaryTreeNode<T> *right;
  8.  
  9.  BinaryTreeNode(T data) {
  10.  this -> data = data;
  11.  left = NULL;
  12.  right = NULL;
  13.  }
  14.  };
  15.  ***************/
  16.  
  17.  
  18. #include <vector>
  19. void ancestor(BinaryTreeNode<int> *root, int k,vector<int>&v){
  20. int ans;
  21. if(root == NULL){
  22. return ;
  23. }
  24. if(root->data == k){
  25. return ;
  26. }
  27. if(root->left->data == k || root->right->data ==k){
  28. ans = root->data;
  29. }
  30. v.push_back(ans);
  31. ancestor(root->left,k,v);
  32. ancestor(root->right,k,v);
  33.  
  34. }
  35. vector<int> ancestors(BinaryTreeNode<int> *root, int k) {
  36. /* Don't write main().
  37.   * Don't read input, they are passed as function arguments.
  38.   * Return output and don't print it.
  39.   * Taking input and printing output is handled automatically.
  40.   */
  41. vector<int>v;
  42. // if(root == NULL){
  43. // return ;
  44. // }
  45.  
  46. ancestor(root,k,v);
  47. return v;
  48. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:19:15: error: variable or field ‘ancestor’ declared void
 void ancestor(BinaryTreeNode<int> *root, int k,vector<int>&v){
               ^~~~~~~~~~~~~~
prog.cpp:19:15: error: ‘BinaryTreeNode’ was not declared in this scope
prog.cpp:19:30: error: expected primary-expression before ‘int’
 void ancestor(BinaryTreeNode<int> *root, int k,vector<int>&v){
                              ^~~
prog.cpp:19:42: error: expected primary-expression before ‘int’
 void ancestor(BinaryTreeNode<int> *root, int k,vector<int>&v){
                                          ^~~
prog.cpp:19:48: error: ‘vector’ was not declared in this scope
 void ancestor(BinaryTreeNode<int> *root, int k,vector<int>&v){
                                                ^~~~~~
prog.cpp:19:48: note: suggested alternative:
In file included from /usr/include/c++/8/vector:64,
                 from prog.cpp:18:
/usr/include/c++/8/bits/stl_vector.h:339:11: note:   ‘std::vector’
     class vector : protected _Vector_base<_Tp, _Alloc>
           ^~~~~~
prog.cpp:19:55: error: expected primary-expression before ‘int’
 void ancestor(BinaryTreeNode<int> *root, int k,vector<int>&v){
                                                       ^~~
prog.cpp:35:1: error: ‘vector’ does not name a type
 vector<int> ancestors(BinaryTreeNode<int> *root, int k) {
 ^~~~~~
stdout
Standard output is empty