fork download
  1. #include<stdio.h>
  2. #include<malloc.h>
  3.  
  4. struct binaryTreeNode{
  5. int data;
  6. struct binaryTreeNode * left;
  7. struct binaryTreeNode * right;
  8. };
  9.  
  10. struct node
  11. {
  12. struct binaryTreeNode * data;
  13. struct node * next;
  14. };
  15.  
  16. struct queue
  17. {
  18. struct node * front;
  19. struct node * rear;
  20. };
  21.  
  22. struct queue * enQueue(struct queue * q, struct binaryTreeNode * num)
  23. {
  24. struct node * temp = (struct node*)malloc(sizeof(struct node));
  25. temp -> data = num;
  26. temp -> next = NULL;
  27. if(q == NULL)
  28. {
  29. q = (struct queue*)malloc(sizeof(struct queue));
  30. q -> front = temp;
  31. }
  32. else
  33. q -> rear -> next = temp;
  34. q -> rear = temp;
  35. //Code obtained from http://w...content-available-to-author-only...s.com
  36. //Feel free to copy but please acknowledge the site wherever possible
  37. return q;
  38. }
  39.  
  40. struct queue * deQueue(struct queue * q)
  41. {
  42. struct node * temp = q->front;
  43. q -> front = q->front->next;
  44. free(temp);
  45. if(q->front == NULL)
  46. return NULL;
  47. else
  48. return q;
  49. }
  50.  
  51. int isQueueEmpty(struct queue * q)
  52. {
  53. if(q)
  54. return 0;
  55. else
  56. return 1;
  57. }
  58.  
  59. int sumOfBinaryTree(struct binaryTreeNode * root)
  60. {
  61. // Terminating condition
  62. if(root == NULL)
  63. {
  64. return 0;
  65. }
  66. else
  67. {
  68. //Recursively call the addition of root + left + right
  69. return (root -> data + sumOfBinaryTree(root -> left) + sumOfBinaryTree(root -> right));
  70. }
  71. }
  72.  
  73. int sumOfBinaryTreeIterative(struct binaryTreeNode * root)
  74. {
  75. // Level order traversal
  76. struct binaryTreeNode * temp = NULL;
  77. struct queue * Q = NULL;
  78.  
  79. // Maintain a count
  80. int sum = 0;
  81.  
  82. if(root == NULL)
  83. return 0;
  84.  
  85. Q = enQueue(Q, root);
  86. while(!isQueueEmpty(Q))
  87. {
  88. temp = Q -> front -> data;
  89.  
  90. // Add it to the sum
  91. sum += temp -> data;
  92.  
  93. Q = deQueue(Q);
  94. if(temp -> left)
  95. Q = enQueue(Q, temp -> left);
  96. if(temp -> right)
  97. Q = enQueue(Q, temp -> right);
  98. }
  99. // Delete the queue
  100. free(Q);
  101.  
  102. // Now return the count
  103. return sum;
  104. }
  105.  
  106. // Test the above functions
  107. int main(void)
  108. {
  109. // Initialize the tree
  110. struct binaryTreeNode * root = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  111. root-> data = 1;
  112. struct binaryTreeNode * l = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  113. l -> data = 2;
  114. struct binaryTreeNode * ll = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  115. ll -> data = 4;
  116. ll -> left = ll -> right = NULL;
  117. struct binaryTreeNode * lr = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  118. lr -> data = 5;
  119. lr -> left = lr -> right = NULL;
  120. l -> left = ll;
  121. l -> right = lr;
  122. struct binaryTreeNode * r = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  123. r -> data = 3;
  124. struct binaryTreeNode * rl = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  125. rl -> data = 6;
  126. rl -> left = rl -> right = NULL;
  127. struct binaryTreeNode * rr = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  128. rr -> data = 7;
  129. rr -> left = rr -> right = NULL;
  130. r -> left = rl;
  131. r -> right = rr;
  132. root -> left = l;
  133. root -> right = r;
  134.  
  135. printf("Sum of nodes = %d",sumOfBinaryTreeIterative(root));
  136.  
  137. return 0;
  138. }
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
Sum of nodes = 28