fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node{
  4. int data;
  5. struct node* left;
  6. struct node* right;
  7. };
  8. struct node* createnode(int val)
  9. {
  10. struct node* p=(struct node*)malloc(sizeof(struct node));
  11. p->data=val;
  12. p->left=NULL;
  13. p->right=NULL;
  14. return p;
  15. }
  16. int max(int a,int b,int c)
  17. {
  18. if(a>b && a>c)
  19. return a;
  20. if(b>a && b>c)
  21. return b;
  22. else
  23. return c;
  24. }
  25.  
  26. int maxnode(struct node* root)
  27. {
  28. if(root==NULL)
  29. return -1;
  30. int lmax=maxnode(root->left);
  31. int rmax=maxnode(root->right);
  32. int res=max(root->data,lmax,rmax);
  33. return res;
  34. }
  35. void preorder(struct node* root)
  36. {
  37. if (root==NULL)
  38. return;
  39. printf("%d ",root->data);
  40. preorder(root->left);
  41. preorder(root->right);
  42. }
  43. void inorder(struct node* root)
  44. {
  45. if (root==NULL)
  46. return;
  47.  
  48. inorder(root->left);
  49. printf("%d ",root->data);
  50. inorder(root->right);
  51. }
  52. void postorder(struct node* root)
  53. {
  54. if (root==NULL)
  55. return;
  56.  
  57. postorder(root->left);
  58. postorder(root->right);
  59. printf("%d ",root->data);
  60. }
  61.  
  62. int main() {
  63. struct node* root=createnode(1);
  64. root->left=createnode(2);
  65. root->left->left=createnode(4);
  66. root->left->right=createnode(5);
  67. root->right=createnode(8);
  68. root->right->left=createnode(6);
  69. root->right->right=createnode(9);
  70. int node= maxnode(root);
  71. printf("Maximum node is=%d \n",node);
  72. preorder(root);
  73. printf("\n");
  74. inorder(root);
  75. printf("\n");
  76. postorder(root);
  77. return 0;
  78. }
  79.  
  80.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Maximum node is=9 
1 2 4 5 8 6 9 
4 2 5 1 6 8 9 
4 5 2 6 9 8 1