fork(1) download
  1. #include <iostream>
  2. #include<stack>
  3. using namespace std;
  4. struct node
  5. {
  6. int data;
  7. struct node *left;
  8. struct node *right;
  9. };
  10. struct node *newNode(int data)
  11. {
  12. struct node *temp=new struct node;
  13. temp->data=data;
  14. temp->left=NULL;
  15. temp->right=NULL;
  16. return temp;
  17. }
  18. int getHeight(struct node *root)
  19. {
  20. if(root==NULL)
  21. return 0;
  22. int lh=getHeight(root->left);
  23. int rh=getHeight(root->right);
  24. if(lh>rh)
  25. return 1+lh;
  26. else
  27. return 1+rh;
  28. }
  29. void inOrder(struct node *root)
  30. {
  31. stack<struct node *>st;
  32. struct node *curr=root;
  33. int done=1;
  34. while(done)
  35. {
  36. if(curr)
  37. {
  38. st.push(curr);
  39. curr=curr->left;
  40. }
  41. else
  42. {
  43. if(!st.empty())
  44. {
  45. curr=st.top();
  46. st.pop();
  47. cout<<curr->data<<" ";
  48. curr=curr->right;
  49. }
  50. else
  51. done=0;
  52. }
  53. }
  54. }
  55. int main() {
  56. struct node *root = newNode(1);
  57. root->left = newNode(2);
  58. root->right = newNode(3);
  59. root->left->left = newNode(4);
  60. root->left->right = newNode(5);
  61.  
  62. inOrder(root);
  63. return 0;
  64. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
4 2 5 1 3