top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Get first node in a binary tree given a particular level?

+1 vote
492 views

Suppose you are given a tree and asked to find out the first node at nth level. How can it be done? C code would be helpful?

posted Feb 19, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Algorithm to find first node of nth level of Tree
Step 1. Traverse the given tree using level order traversal method
Step 1.a check n level of given tree.

if(nth level is find)
     print the first node of given tree.
else
    print: the nth level is not present in the list.

2 Exit.

answer Feb 19, 2016 by Rajan Paswan
But how to find first node that was the query...Do you like to give the code also.

PS: Completeness of answer not only helps others but helps you also as it attract recruiters on your profile.
yes i will send code as well as very soon....
Similar Questions
+3 votes

Given a binary tree print it in inward spiral order i.e first print level 1, then level n, then level 2, then n-1 and so on.

For Ex -

         1 
   2           3 
 4    5       6     7 
8 9  10 11  12 13  14 15 

Print- 1 15 14 13 12 11 10 9 8 2 3 7 6 5 4

+6 votes

Given binary tree is:

        1
     /     \
    2        3
  /  \        \
 4   5         6
             /   \
            7     8

The output should be 1, 2, 3, 4, 5, 6, 7, 8

...