fork download
  1. void PrintKDistanceInSubtree(Node* root, int k)
  2. {
  3. if(root == NULL) return;
  4.  
  5. if(k == 0)
  6. {
  7. cout<<root->data<<" ";
  8. }
  9. else
  10. {
  11. PrintKDistanceInSubtree(root->left,k-1);
  12. PrintKDistanceInSubtree(root->right,k-1);
  13. }
  14. }
  15.  
  16. int mainfunction(Node* root, int x, int k)
  17. {
  18.  
  19. if(root == NULL) return -1;
  20.  
  21. if(root->data == x)
  22. {
  23. return 0; //your are at 0 distance from that node
  24. }
  25.  
  26. int l1 = mainfunction(root->left,x,k);
  27. int l2 = mainfunction(root->right,x,k);
  28.  
  29. if(l1 != -1 && l1 < k)
  30. {
  31. cout<<root->data;
  32. if(root->right != NULL)
  33. {
  34. PrintKDistanceInSubtree(root->right,k-x-1);
  35. }
  36. return l1+1;
  37. }
  38.  
  39. if(l2 != -1 && l2 < k)
  40. {
  41. cout<<root->data;
  42. if(root->left != NULL)
  43. {
  44. PrintKDistanceInSubtree(root->left,k-x-1);
  45. }
  46. return l2+1;
  47. }
  48.  
  49. return -1;
  50.  
  51. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:30: error: unknown type name 'Node'
 void PrintKDistanceInSubtree(Node* root, int k)
                              ^
prog.c:16:18: error: unknown type name 'Node'
 int mainfunction(Node* root, int x, int k)
                  ^
stdout
Standard output is empty