fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef struct btNode
  5. {
  6. btNode *left;
  7. btNode *right;
  8. int data;
  9. }btNode;
  10.  
  11.  
  12. btNode *create_btNode(int data)
  13. {
  14. btNode *temp = new btNode[1];
  15. temp->data = data;
  16. temp->left = temp->right = NULL;
  17. }
  18.  
  19. void recFunc(btNode *temp,int type)
  20. {
  21. if((temp->left == NULL)&&(temp->right == NULL))cout << temp->data << " ";
  22. else if(type == 1)cout << temp->data << " ";
  23. else {;}
  24.  
  25.  
  26. if(type == 3)
  27. {
  28. if(temp->left)recFunc(temp->left,0);
  29. if(temp->right)recFunc(temp->right,3);
  30. cout << temp->data << " ";
  31. }
  32. else if(type == 1)
  33. {
  34. if(temp->left)recFunc(temp->left,1);
  35. if(temp->right)recFunc(temp->right,0);
  36. }
  37. else if(type == 0)
  38. {
  39. if(temp->left)recFunc(temp->left,0);
  40. if(temp->right)recFunc(temp->right,0);
  41. }
  42. else {;}
  43. }
  44.  
  45. void func(btNode *root)
  46. {
  47. if(root != NULL)
  48. {
  49. cout << root->data << " ";
  50. if(root->left)recFunc(root->left,1);
  51. if(root->right)recFunc(root->right,3);
  52. cout << endl;
  53. }
  54. }
  55.  
  56.  
  57. int main()
  58. {
  59. btNode *root = create_btNode(20);
  60. root->left = create_btNode(8);
  61. root->left->left = create_btNode(4);
  62. root->left->right = create_btNode(12);
  63. root->left->right->left = create_btNode(10);
  64. root->left->right->right = create_btNode(14);
  65. root->right = create_btNode(22);
  66. root->right->right = create_btNode(25);
  67. func(root);
  68. return 0;
  69. }
  70.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty