fork(1) download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. struct node {
  6. int data;
  7. node *left;
  8. node *right;
  9. };
  10.  
  11. node *insert(node *root,int d)
  12. {
  13. if(root==NULL)
  14. {
  15. node *temp=new node();
  16. temp->data=d;
  17. temp->left=temp->right=NULL;
  18. root=temp;
  19.  
  20. }
  21. else
  22. { if(root->data>d)
  23. {
  24. root->left=insert(root->left,d);
  25. }
  26. else
  27. root->right=insert(root->right,d);
  28. }
  29. return root;
  30. }
  31.  
  32. int height(node *root)
  33. {
  34. if(root==NULL)
  35. return 0;
  36.  
  37. int left=height(root->left);
  38. int right=height(root->right);
  39. return max(left,right)+1;
  40. }
  41.  
  42. int main() {
  43. node *root=NULL;
  44. root=insert(root,5);
  45. root=insert(root,17);
  46. root=insert(root,34);
  47. root=insert(root,2);
  48. int x=height(root);
  49. cout << x << endl;
  50. return 0;
  51. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
3