GeeksforGeeks » Interview Questions
Amazon Interview Question for Software Engineer/Developer (Fresher)
(6 posts)-
Given a binary tree write efficient code to make it balance binary tree..interview wants from me clear code & appropach
-
Looks like an AVL rotation question.
Start from root and recursively apply AVL rotations.
-
@kartik...i am not able to do dat can u provide the code for that..thnx
-
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. -
@kartik..can you plz post the code of AVL tree Rotation..e.g rotating tree in left or right..??
-
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.