Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Suppose the double linked list representation is 12345678. The binary tree should be
---------------1-----------------
--------3-----------2---------------
---4------5-----6-------7
-----------------------------8
And yes you can use one stack to solve the problem. Sorry i used the word in place . I have edited the question now.
How about this approach? A pathetically tedious one though.
Let L denote the level of a tree starting from 0. We can make use of 2 pointers to denote the start and end of the nodes in a level. These pointers need to be maintained at the current level as well as at the parent level. Assuming that we need to build a complete binary tree, we can process the nodes of DLL in numbers that are powers of 2.
At L = 0, we have 2^L = 2^0 = 1 node. The start and end pointers will point to this node.
At L = 1, we have 2^L = 2^1 = 2 nodes. L is odd, we push the elements of this level on to a stack. While pushing elements on to the stack, we reverse the previous/next pointers of the nodes. This again is a tedious process, where before pushing one element, we pop the previous element, change the pointers and then push 2 elements on to the stack. While popping the elements from a stack attach two elements at a time to nodes at parent level and keep advancing the start pointer of the parent level until it reaches the end pointer. At this level again, we have maintained pointers that denote the start and end of the nodes in this level.
At L = 2, we have 2^2 = 4 nodes. L is even here. No need to use a stack here and attach 2 nodes at a time to nodes at the parent level and keep advancing start pointer of the parent level until it reaches the end pointer
Repeat the above steps until the end level L=K is reached. At this level, if k is odd, then push 2^K elements on to the stack, NULLs being the "padding" nodes. Then attach the nodes to the nodes at the parent level.
Again, terribly tedious process, but just an attempt to solve the problem.
this can be done with the prev pointer without using the stack (even one stack)
it call fillRight and fillLeft alternatively in which there are two pointers
p iterates along the current level with the prev pointer and r iterates along the next level with the next
fill the left and right child accordingly and return the last node of the next level
for example
level 1: fill node 1 and return the last node 3
leve 2: fill node 3 and then 2 and return the last node 7
level 3: fill node 7 and return the last node 8
leve 4: fille node 8 and return null
void fillRight(ListNode node)
{
ListNode p = node; // p iterate along the current level using the prev pointer
ListNode prev = null;
ListNode r = p.next; // r iterate along the next level using the next pointer
if (r != null)
r.prev = null;
while (p != null) // fill the right child first
prev = p.prev;
p.next = r // next for right
if (r != null)
r = r.next;
p.prev = r; // prev for left
if (r != null)
r = r.next;
p = prev;
}
return r;
}
void fillLeft(ListNode node)
{
ListNode p = node;
ListNode prev = null;
ListNode r = p.next;
if (r != null)
r.prev = null;
while (p != null) { // fill the left child first
prev = p.prev;
p.prev = r; // prev for left
if (r != null)
r = r.next;
p.next = r; // next for right
if (r != null)
r = r.next;
p = prev;
}
return r;
}
void convert(ListNode head)
{
ListNODE p = head;
boolean right = true;
while (p != null) {
ListNode q;
if(right) {
q = fillRight(p);
} else {
q = fillLeft(p);
}
right = !right;
p = q;
}
}
ok...lets try with an example
NULL-1-2-3-4-5-6-7-8-NULL
1.clearly root of tree will be the head
p,q,current node are say temp nodes
p=head(1 in this example)
now add 2 and 3 as left and right children of 1
2.q=last node added as a child
current node=q;
now while(current node is p)
add 2 children to current nodes
current node=current node->prev
3.p =q,
q=last node added as a child
go to step 2
hope this make sense.may be bit confusing,but atleast the approach,u can get
please note that once we need to make left child first and then right child,then vice versa, We can use a flag variable to ensure that
1. We can use one stack. initialise i=0 and flag=0 stack and queue
2. if(flag==0)
insert 2^i elements and elements from stack into the queue and set flag=~flag
else
insert 2^i elements into the stack and remove element one by one and insert into the tree..set flag=~flag
3. do the above step until DLL is empty
This one uses created tree nodes to keep temporary information as tree is built, then clears leftovers. No additional stack is used.
I think that code can be made more compact.
int serialized[] = { 1, 3, 2, 4, 5, 6, 7, 10, 9, 8 };
int length = sizeof(serialized)/sizeof(int);
TreeNode *root = new TreeNode();
root->data=serialized[0];
TreeNode *currentParent = root;
TreeNode *prevCreated = 0;
bool currentParentAssigned = false;
bool goLeft = true;
for(int i = 1;i < length; ++i)
{
TreeNode *node = new TreeNode();
node->data = serialized[i];
if (goLeft)
{
node->right = prevCreated;
}
else
{
node->left = prevCreated;
}
if (currentParentAssigned)
{
TreeNode *nextParent=goLeft ? currentParent->left : currentParent->right;
if (goLeft)
{
currentParent->left = node;
}
else
{
currentParent->right = node;
}
currentParent = nextParent;
currentParentAssigned=false;
}
else
{
if (goLeft)
{
currentParent->right = node;
}
else
{
currentParent->left = node;
}
currentParentAssigned = true;
}
prevCreated = node;
if (currentParent == 0)
{
currentParent = node;
prevCreated = 0;
goLeft = !goLeft;
currentParentAssigned=false;
}
}
for(int i=0;i<2;++i)
{
while(currentParent != 0)
{
TreeNode *nextParent=goLeft ? currentParent->left : currentParent->right;
if (goLeft)
{
currentParent->left = 0;
}
else
{
currentParent->right = 0;
}
currentParent = nextParent;
}
currentParent = prevCreated;
goLeft = !goLeft;
}
Can you explain a little bit more, maybe with an example?
- oOZz July 30, 2013What is the expected space complexity, because you said the solution has to be in place and then you're asking, if it can be done with one stack? I think you're confusing the problem with the spiral traversal order of a binary tree, which is done with 2 stacks, but this question is the other way around.