GeeksforGeeks » Interview Questions

Amazon Interview Question for Software Engineer/Developer (Fresher)

(6 posts)

Tags:

  1. rama
    guest
    Posted 1 year ago #

    Given a binary tree write efficient code to make it balance binary tree..interview wants from me clear code & appropach

  2. kartik
    Moderator
    Posted 1 year ago #

    Looks like an AVL rotation question.

    Start from root and recursively apply AVL rotations.

  3. rama
    guest
    Posted 1 year ago #

    @kartik...i am not able to do dat can u provide the code for that..thnx

  4. iictarpit
    Member
    Posted 1 year ago #

    1. we can traverse the given binary tree in DFS/BFS way and store them in an array of (Node* array[] ) type.
    2. Now traverse the array and set the pointer of the right child and left child of ith element to 2i and 2i th element in the array.

  5. rama
    guest
    Posted 1 year ago #

    @kartik..can you plz post the code of AVL tree Rotation..e.g rotating tree in left or right..??

  6. gaurav
    Member
    Posted 10 months ago #

    1. Count the number of nodes using any traversal technique
    2. If there are N nodes make an array of Nodes of size N and store all the nodes in this array using BFS
    3. In the array,
    for i = 0 to N/2
    {
    nodes[i]->right = &nodes[2*i+1];
    nodes[i]->left = &nodes[2*i+2];
    }

    Don't forget to check if 2i+1 and 2i+2 are within array limits 0 to N-1

    This technique requires linear time. O(N)


Reply

You must log in to post.

RSS feed for this topic