• Source
    1. #include <bits/stdc++.h>
    2.  
    3. using namespace std;
    4.  
    5. struct BstNode
    6. {
    7. long data;
    8.  
    9. BstNode* left;
    10.  
    11. BstNode* right;
    12.  
    13. };
    14.  
    15. BstNode* GetNewNode(long data)
    16. {
    17. BstNode* newNode = new BstNode();
    18.  
    19. newNode->data = data;
    20.  
    21. newNode->left = newNode->right = NULL;
    22.  
    23. return newNode;
    24. }
    25.  
    26. BstNode* Insert(BstNode* root, long data)
    27. {
    28. if(root==NULL)
    29. {
    30. root = GetNewNode(data);
    31. }
    32. else if(data <= root->data)
    33. {
    34. root->left = Insert(root->left, data);
    35. }
    36. else
    37. {
    38. root->right = Insert(root->right, data);
    39. }
    40.  
    41. return root;
    42.  
    43. }
    44.  
    45. void printPostOrder(BstNode* root)
    46. {
    47. if(root==NULL)
    48. {
    49. return;
    50. }
    51.  
    52. printPostOrder(root->left);
    53.  
    54. printPostOrder(root->right);
    55.  
    56. printf("%ld\n", root->data);
    57. }
    58.  
    59. int main()
    60. {
    61. BstNode* root = NULL;
    62.  
    63. long n, data,i;
    64.  
    65. while(scanf("%ld", &data)!=EOF)
    66. {
    67. root = Insert(root, data);
    68. }
    69.  
    70. printPostOrder(root);
    71.  
    72. return 0;
    73. }
    74.