Tuesday, February 19, 2013
C Program to Print level order traversal of a binary tree using queue.
To print the level order traversal of the tree we need to use Queue .For the above binary tree in the diagram level order traversal is ABCDEFG .
Solution is very simple . The steps are
1.Add root node to the queue using enqueue function
2.Get a node from queue using dequeue function and print it . If the node has children add it to the queue (enqueue)
3.Continue the step 2 until the queue is empty.
Assume that enqueue(node) funtion adds node into queue and dequeue() function deletes the node from the queue and returns node.And dequeue() function returns null when there is no node in the queue .
Code: (in C)
For the above tree execution will be
in queue : A output:
in queue : BC output:A
in queue : CDE output:AB
in queue : DEFG output:ABC
in queue : EFG output:ABCD
in queue : FG output:ABCDE
in queue : G output:ABCDEF
in queue : output:ABCDEFG
Please let me know if u have any questions .
Saturday, February 16, 2013
C program to print first n fibonacci numbers
Fibonacci series is formed by adding two of its previous numbers .The first 2 fibonacci numbers are 0 and 1 . Series 0,1,1,2,3,5,8... is an example of fibonacci series .
Input : 7
Output: 0 1 1 2 3 5 8
Code: (in C)
Please let me know if you have any questions .
Input : 7
Output: 0 1 1 2 3 5 8
Code: (in C)
Please let me know if you have any questions .
Amazon question - Schedule a meeting based on given work timings
Consider the following input and output . In the input, first line contains number of input 5 and the time you need to schedule (120 minutes) . Next five lines contains the work timings of that person , so he is busy at these times . And the output shows the list of available timings. This question is asked by amazon in interviewstreet.com . First the timings are sorted using bubble sort based on start time .And you need to compare timings to get the available timings .
Sample Input:
5 120
16 00 17 00
10 30 14 30
20 45 22 15
10 00 13 15
09 00 11 00
Sample Output:
00 00 09 00
17 00 20 45
Code: (in JAVA)
Please let me know if you have any questions .
Sample Input:
5 120
16 00 17 00
10 30 14 30
20 45 22 15
10 00 13 15
09 00 11 00
Sample Output:
00 00 09 00
17 00 20 45
Code: (in JAVA)
Please let me know if you have any questions .
Subscribe to:
Posts (Atom)