fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct TreeNode
  5. {
  6. char data;
  7. TreeNode* left;
  8. TreeNode* right;
  9. };
  10.  
  11. TreeNode* createNode(char data)
  12. {
  13. TreeNode* temp = (TreeNode *)malloc(sizeof(TreeNode));
  14. temp->data = data;
  15. temp->left = temp->right = NULL;
  16. return temp;
  17. }
  18.  
  19. void printTree(TreeNode* root, int space = 0)
  20. {
  21. if (root == NULL)
  22. return;
  23.  
  24. // Increase distance between levels
  25. int COUNT = 5;
  26. space += COUNT;
  27.  
  28. // Print right child first
  29. printTree(root->right, space);
  30.  
  31. // Print current node after space count
  32. printf("\n");
  33. for (int i = COUNT; i < space; i++)
  34. printf(" ");
  35. printf("%c\n", root->data);
  36.  
  37. // Print left child
  38. printTree(root->left, space);
  39. }
  40.  
  41.  
  42. void preorder(TreeNode* root)
  43. {
  44. if(root==NULL) return;
  45.  
  46. printf("%c ", root->data);
  47. preorder(root->left);
  48. preorder(root->right);
  49. }
  50.  
  51.  
  52. void inorder(TreeNode* root)
  53. {
  54. if(root==NULL) return;
  55.  
  56. inorder(root->left);
  57.  
  58. printf("%c ", root->data);
  59.  
  60. inorder(root->right);
  61. }
  62.  
  63. void postorder(TreeNode* root)
  64. {
  65. if(root==NULL) return;
  66.  
  67. postorder(root->left);
  68. postorder(root->right);
  69. printf("%c ", root->data);
  70. }
  71.  
  72. int height(TreeNode* root)
  73. {
  74. if(root==NULL) /// root NULL
  75. {
  76. return 0;
  77. }
  78. else if(root->left==NULL && root->right==NULL) /// child count: 0
  79. {
  80. return 0;
  81. }
  82. else
  83. {
  84. if(root->left!=NULL && root->right!=NULL) /// child count: 2
  85. {
  86. int left_side_height = height(root->left);
  87. int right_side_height = height(root->right);
  88.  
  89. if(left_side_height>right_side_height)
  90. {
  91. return left_side_height + 1;
  92. }
  93. else
  94. return right_side_height + 1;
  95.  
  96. }
  97. else if(root->left!=NULL) /// child count: 1 (Left Child)
  98. {
  99. int left_side_height = height(root->left);
  100. return left_side_height + 1;
  101. }
  102. else /// child count: 1 (Right Child)
  103. {
  104. int right_side_height = height(root->right);
  105. return right_side_height + 1;
  106. }
  107. }
  108. }
  109.  
  110.  
  111. int findValue(TreeNode* root, char value)
  112. {
  113.  
  114. }
  115.  
  116. int countNodes(TreeNode* root)
  117. {
  118.  
  119. }
  120.  
  121. int countLeaves(TreeNode* root)
  122. {
  123.  
  124. }
  125.  
  126. int main()
  127. {
  128. TreeNode* root = createNode('A');
  129.  
  130. root->left = createNode('B');
  131. root->right = createNode('C');
  132.  
  133. root->left->left = createNode('D');
  134. root->left->right = createNode('E');
  135.  
  136. root->right->left = createNode('F');
  137. root->right->right = createNode('G');
  138.  
  139. root->right->right->left = createNode('H');
  140.  
  141.  
  142. /// printTree(root);
  143. /// preorder(root);
  144. /// inorder(root);
  145.  
  146. /// postorder(root);
  147. printf("%d\n", height(root));
  148.  
  149. return 0;
  150. }
  151.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
3