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. struct binaryTreeNode * deepestNode(struct binaryTreeNode * root)
  60. {
  61. // Level order traversal
  62. struct binaryTreeNode * temp = NULL;
  63. struct queue * Q = NULL;
  64.  
  65. if(root == NULL)
  66. return;
  67. Q = enQueue(Q, root);
  68. while(!isQueueEmpty(Q))
  69. {
  70. temp = Q -> front -> data;
  71. Q = deQueue(Q);
  72. if(temp -> left)
  73. Q = enQueue(Q, temp -> left);
  74. if(temp -> right)
  75. Q = enQueue(Q, temp -> right);
  76. }
  77. // Delete the queue
  78. free(Q);
  79.  
  80. // Now return the last node
  81. return temp;
  82. }
  83.  
  84. // Test the above functions
  85. int main(void)
  86. {
  87. // Initialize the tree
  88. struct binaryTreeNode * root = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  89. root-> data = 1;
  90. struct binaryTreeNode * l = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  91. l -> data = 2;
  92. struct binaryTreeNode * ll = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  93. ll -> data = 4;
  94. ll -> left = ll -> right = NULL;
  95. struct binaryTreeNode * lr = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  96. lr -> data = 5;
  97. lr -> left = lr -> right = NULL;
  98. l -> left = ll;
  99. l -> right = lr;
  100. struct binaryTreeNode * r = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  101. r -> data = 3;
  102. struct binaryTreeNode * rl = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  103. rl -> data = 6;
  104. rl -> left = rl -> right = NULL;
  105. struct binaryTreeNode * rr = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  106. rr -> data = 7;
  107. rr -> left = rr -> right = NULL;
  108. r -> left = rl;
  109. r -> right = rr;
  110. root -> left = l;
  111. root -> right = r;
  112.  
  113. struct binaryTreeNode * deepest = deepestNode(root);
  114.  
  115. printf("Deepest node = %d",deepest->data);
  116.  
  117. return 0;
  118. }
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
Deepest node = 7