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. if(root==NULL) return 0;
  114. else if(root->data==value) return 1;
  115. else
  116. {
  117. int left_answer = findValue(root->left, value);
  118. int right_answer = findValue(root->right, value);
  119.  
  120. return left_answer || right_answer;
  121. }
  122. }
  123.  
  124. int countNodes(TreeNode* root)
  125. {
  126. if(root==NULL) return 0;
  127. else
  128. {
  129. int left_answer = countNodes(root->left);
  130. int right_answer = countNodes(root->right);
  131. return left_answer + right_answer + 1;
  132. }
  133. }
  134.  
  135. int countLeaves(TreeNode* root)
  136. {
  137. if(root==NULL) return 0;
  138. else if(root->left==NULL && root->right==NULL) return 1;
  139. else
  140. {
  141. int left_answer = countLeaves(root->left);
  142. int right_answer = countLeaves(root->right);
  143. return left_answer + right_answer;
  144. }
  145. }
  146.  
  147. int main()
  148. {
  149. TreeNode* root = createNode('A');
  150.  
  151. root->left = createNode('B');
  152. root->right = createNode('C');
  153.  
  154. root->left->left = createNode('D');
  155. root->left->right = createNode('E');
  156.  
  157. root->right->left = createNode('F');
  158. root->right->right = createNode('G');
  159.  
  160. root->right->right->left = createNode('H');
  161.  
  162.  
  163. /// printTree(root);
  164. /// preorder(root);
  165. /// inorder(root);
  166.  
  167. /// postorder(root);
  168. printf("%d\n", height(root));
  169.  
  170. return 0;
  171. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
3