AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: rickterfj — October 6, 2006
    -12 votes
      + -

    Use recursion to print in post-order for the binary tree.

  2. Submitted By: aansari — October 6, 2006
    -1 votes
      + -

    My solution demands a lot of extra space, but here it goes. I’m assuming each node holds one printable character, you can abstract this algorithm to hold whatever you want.

    char levels_char_buffer[MAX_LEVELS][MAX_BUFFER]; //hold the information for each level of the tree.

    void print_tree_level( tree* treep, int level)
    {
    strcat( levels_char_buffer[ level], p->data);
    print_tree_level( treep->leftp, level+1);
    print_tree_level( treep->rightp, level+1);
    }

    void print_tree( tree* treep)
    {
    memset( level_char_buffer, ‘’, sizeof( level_char_buffer); //initialize buffer
    print_tree_level( treep, 0);

    //dump buffer out
    for ( level = 0; level < MAX_LEVEL; level++)
    {
    printf( “level%d: %sn”, level, level_char_buffer[level]);
    }
    }

  3. Submitted By: wrong_answer — October 6, 2006
    +16 votes
      + -

    use a breadth-first search…are u guys retarded or something?

  4. Submitted By: cherku — October 6, 2006
    +3 votes
      + -

    As you start from the top put each element in
    a queue and also its children first left and then right.After you are done Dequeue all the elements.That’s it.

  5. Submitted By: TimeHawk — October 6, 2006
    -5 votes
      + -

    Traverse the tree in pre-order using say an NSTAK traversal method.

  6. Submitted By: devil — October 6, 2006
    -24 votes
      + -

    I think all the above answers are wrong.

    If you look at the way the binary tree is
    stored, its stored level by level.
    So you just need to print out the characters
    in a array :-) I guess!!!!

  7. Submitted By: rdakka — October 6, 2006
    -2 votes
      + -

    void level_order(node* n)
    {
    if (n)
    {
    stack<node*> s1,s2;
    stack<node*> *ps1, *ps2;
    int flag = true;
    s1.push(n->right);
    s1.push(n->left);
    cout << *n << ” “;
    do
    {
    if (flag)
    {
    ps1 = &s1;
    ps2 = &s2;
    }
    else
    {
    ps1 = &s2;
    ps2 = &s1;
    }
    while (!ps1->empty())
    {
    n = ps1->top();
    ps1->pop();
    if (n)
    {
    cout << *n << ” “;
    ps2->push(n->right);
    ps2->push(n->left);
    }
    }
    flag = !flag;
    }
    while(!ps2->empty());
    }
    }

  8. Submitted By: Luke — October 6, 2006
    +1 votes
      + -

    Imagine the next question would be that you’re not allowed to use external storage? So you can’t use breadth-first (which seems the most obvious solution in this case).

    In that case you have to write a function to find the node next to it. Little bit less trivial if the tree is not balanced.

  9. Submitted By: Ajay — December 6, 2006
    -6 votes
      + -

    int Printlbyl(node *head)
    {

    if (head != NULL)
    {
    printf(head->info)
    Printlbyl(head->left)
    Printlbyl(head->right)
    }
    else
    return;

    }

  10. Submitted By: hector — December 29, 2006
    +10 votes
      + -

    void breadthFirstPrint(node *root)
    {
    Queue q = new Queue();
    node *temp;
    q.push(root);
    while(!q.isEmpty())
    {
    temp = q.pop();
    printf(”%d”,temp->info);
    if(temp->left)
    q.push(temp->left);
    if(temp->right)
    q.push(temp->right);

    }
    }

    This follows the same principle as breadthfirst search. The right answer is to use a Queue.

  11. Submitted By: jijiduru — March 8, 2007
    -2 votes
      + -

    The BFS solution is good, but if you’re not allowed to use external memory, then you use a normal inorder (for example) and do something like this (in pseudocode):

    (global) level

    void inorder(Tree b,integer _level)
    if (b)
    if _level=level
    print b
    level=level+1
    inorder(b.left,_level)
    inorder(b.right,level)
    level=level-1

    The complexity is O(h*n) where h is the height of the tree

    cheers

  12. Submitted By: Murali Chilukuri — April 5, 2007
    -1 votes
      + -

    int print_level(struct node *temp, int level) {
    if (temp == NULL) {
    return 0;
    } if (level == 1) {
    printf(”%d\t”,temp->data);
    return 0;
    } else {
    print_level(temp->left, level-1);
    print_level(temp->right, level-1);
    return 0;
    }
    }

    void print(void) {
    int i = 1;
    int temp = hight(root);

    for(i = 1; i

  13. Submitted By: Divya — July 9, 2007
    +2 votes
      + -

    Traverse the elements as you do in a BFS.
    A pseudo code:

    while(node->left!=NULL || node->right!-NULL)
    {
    push(node->left) into the Queue//print the value
    push(node->right) into the queue//print the value
    node=pop()
    }

  14. Submitted By: Satish — October 22, 2007
    not yet rated
      + -

    If the tree is extremely large, external storage is likely to be disallowed by the interviewer.
    BFS (using a queue) is ruled out.
    Recursive DFS (Inorder) called for level = 1 to MAX_HEIGHT works but is inefficient because its called fail (stack memory overflow).

    The right solution will be to traverse only once using PreOrder DFS, keep track of tree level during recursive calls and append the visited node content into an external file named like 1.txt for level 1 and 2.txt for level 2 etc.

    Finally read all the files 1.txt,…,MAX_HEIGHT.txt and print contents.

    File IO might be slightly slower but Async IO takes away some pain. And makes it faster than multiple traversal for each level.

  15. Submitted By: Satish — October 23, 2007
    not yet rated
      + -

    Typo in 1st Paragraph:
    Recursive DFS (Inorder) called for level = 1 to MAX_HEIGHT works but is inefficient because its called MAX_HEIGHT times.
    int MAX_HEIGHT = GetTreeHeight(Node* Tree);
    for (int height = 1 to MAX_HEIGHT)
    {
    VisitNodesAtHeight(height)
    }

  16. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.