fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<limits.h>
  4.  
  5. struct TreeNode
  6. {
  7. int data;
  8. struct TreeNode* left;
  9. struct TreeNode* right;
  10. };
  11.  
  12. typedef struct TreeNode TreeNode;
  13.  
  14. TreeNode* createNode(int data)
  15. {
  16. TreeNode* temp = (TreeNode *)malloc(sizeof(TreeNode));
  17. temp->data = data;
  18. temp->left = temp->right = NULL;
  19. return temp;
  20. }
  21.  
  22. void printTree(TreeNode* root, int space)
  23. {
  24. if (root == NULL)
  25. return;
  26.  
  27. // Increase distance between levels
  28. int COUNT = 5;
  29. space += COUNT;
  30.  
  31. // Print right child first
  32. printTree(root->right, space);
  33.  
  34. // Print current node after space count
  35. printf("\n");
  36. for (int i = COUNT; i < space; i++)
  37. printf(" ");
  38. printf("%d\n", root->data);
  39.  
  40. // Print left child
  41. printTree(root->left, space);
  42. }
  43.  
  44.  
  45. void preorder(TreeNode* root)
  46. {
  47. if(root==NULL) return;
  48.  
  49. printf("%d ", root->data);
  50. preorder(root->left);
  51. preorder(root->right);
  52. }
  53.  
  54.  
  55. void inorder(TreeNode* root)
  56. {
  57. if(root==NULL) return;
  58.  
  59. inorder(root->left);
  60.  
  61. printf("%d ", root->data);
  62.  
  63. inorder(root->right);
  64. }
  65.  
  66. void postorder(TreeNode* root)
  67. {
  68. if(root==NULL) return;
  69.  
  70. postorder(root->left);
  71. postorder(root->right);
  72. printf("%d ", root->data);
  73. }
  74.  
  75. int height(TreeNode* root)
  76. {
  77. if(root==NULL) /// root NULL
  78. {
  79. return 0;
  80. }
  81. else if(root->left==NULL && root->right==NULL) /// child count: 0
  82. {
  83. return 0;
  84. }
  85. else
  86. {
  87. if(root->left!=NULL && root->right!=NULL) /// child count: 2
  88. {
  89. int left_side_height = height(root->left);
  90. int right_side_height = height(root->right);
  91.  
  92. if(left_side_height>right_side_height)
  93. {
  94. return left_side_height + 1;
  95. }
  96. else
  97. return right_side_height + 1;
  98.  
  99. }
  100. else if(root->left!=NULL) /// child count: 1 (Left Child)
  101. {
  102. int left_side_height = height(root->left);
  103. return left_side_height + 1;
  104. }
  105. else /// child count: 1 (Right Child)
  106. {
  107. int right_side_height = height(root->right);
  108. return right_side_height + 1;
  109. }
  110. }
  111. }
  112.  
  113.  
  114. int findValue(TreeNode* root, int value)
  115. {
  116. if(root==NULL) return 0;
  117. else if(root->data==value) return 1;
  118. else
  119. {
  120. int left_answer = findValue(root->left, value);
  121. int right_answer = findValue(root->right, value);
  122.  
  123. return left_answer || right_answer;
  124. }
  125. }
  126.  
  127. int countNodes(TreeNode* root)
  128. {
  129. if(root==NULL) return 0;
  130. else
  131. {
  132. int left_answer = countNodes(root->left);
  133. int right_answer = countNodes(root->right);
  134. return left_answer + right_answer + 1;
  135. }
  136. }
  137.  
  138. int countLeaves(TreeNode* root)
  139. {
  140. if(root==NULL) return 0;
  141. else if(root->left==NULL && root->right==NULL) return 1;
  142. else
  143. {
  144. int left_answer = countLeaves(root->left);
  145. int right_answer = countLeaves(root->right);
  146. return left_answer + right_answer;
  147. }
  148. }
  149.  
  150. int max2(int a, int b)
  151. {
  152. return a > b ? a : b;
  153. }
  154.  
  155. int max3(int a, int b, int c)
  156. {
  157. return max2(max2(a,b),c);
  158. }
  159.  
  160. int findMaxValue(TreeNode *root)
  161. {
  162. if(root==NULL)
  163. {
  164. return INT_MIN;
  165. }
  166. else
  167. {
  168. int left_maximum = findMaxValue(root->left);
  169. int right_maximum = findMaxValue(root->right);
  170. int final_maximum = max3(left_maximum, right_maximum, root->data);
  171. return final_maximum;
  172. }
  173. }
  174.  
  175. TreeNode* mirrorTree(TreeNode *root)
  176. {
  177. if(root==NULL)
  178. {
  179. return root;
  180. }
  181. else
  182. {
  183. TreeNode* temp = root->left;
  184. root->left = root->right;
  185. root->right = temp;
  186.  
  187. root->left = mirrorTree(root->left);
  188. root->right = mirrorTree(root->right);
  189. return root;
  190. }
  191. }
  192.  
  193. int sumInternalValues(TreeNode* root)
  194. {
  195. if(root==NULL)
  196. return 0;
  197. else if(root->left!=NULL || root->right!=NULL)
  198. {
  199. return root->data + sumInternalValues(root->left) + sumInternalValues(root->right);
  200. }
  201. else
  202. {
  203. return 0;
  204. }
  205. }
  206.  
  207.  
  208. int main()
  209. {
  210. TreeNode* root = createNode(10);
  211.  
  212. root->left = createNode(20);
  213. root->right = createNode(30);
  214.  
  215. root->left->left = createNode(40);
  216. root->left->right = createNode(50);
  217.  
  218. root->right->left = createNode(60);
  219. root->right->right = createNode(70);
  220.  
  221. root->right->right->left = createNode(80);
  222.  
  223. printf("Tree:\n");
  224. printTree(root, 0);
  225.  
  226. printf("\n");
  227.  
  228. root = mirrorTree(root);
  229.  
  230. printf("Tree:\n");
  231. printTree(root, 0);
  232.  
  233. printf("\n");
  234.  
  235.  
  236. printf("%d\n", sumInternalValues(root));
  237.  
  238. return 0;
  239. }
  240.  
Success #stdin #stdout 0s 5272KB
stdin
Standard input is empty
stdout
Tree:

          70

               80

     30

          60

10

          50

     20

          40

Tree:

          40

     20

          50

10

          60

     30

               80

          70

130