Microsoft Interview Question
1,272 Interview Reviews |
Back to all Microsoft Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Development Engineer at Microsoft:
Given inorder and preorder traversals of a tree, reconstruct the tree.
See more for this Microsoft Software Development Engineer Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (2)
Inorder traversal: ( c b f d g a e ) (left root right)
The first element of the preorder string is "a" and this is the root of our tree.
If we split inorder string at letter "a" (which is the first element of the preorder string) then we get
( c b f d g ) and ( e ) which are the inorder traversals of the left and right sub trees respectively.
This property can combined with recursion to construct the whole tree.
This concept is clearly explained here with diagram. Simple code using recursion in JAVA is also given
nobrainer .co .cc
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 0 people found this helpful
by Valentino:
A Recursion function with input paramters as preOrder[] and inOrder[]
stop if preOrder is null or preOrder length is 0
Create In Order Elements for Left Side
Create In Order Elements for Right Side
Create Pre Order Elements for Left Side
Create Pre Order Elements for Right Side
Create the node and using recursion assign the value to Left and Right nodes
I guess the key here it to ask what can be considered for reference when creating the tree
for instance
PreOrder 1,2,4,5,3,6,7 <-- First element is the parent for the split sub array
InOrder 4,2,5,1,6,3,7 <-- the parent element is in between the left and right arrays
if we consider unique values this approach can work or we can change it to use the pointer or reference for
each node, I mean if the array has the value and the pointer I guess those are the keys to identify the solution.
Correctme if I'm wrong but how can we rebuilt a tree with a specific structure if all elements are the same.
Here it's my code, I'm using List<int> but with some refactoring we could avoid then and use start and end indexes.
public TreeNode FromInOrderAndPreOrder(int[] preOrder, int[] inOrder)
{
if (preOrder == null || preOrder.Length == 0)
return null;
int nodeValue = preOrder[0];
Dictionary<int, int> rightInOrderDic = new Dictionary<int, int>();
int index = 0;
// Create In Order Elements for Left Side
List<int> leftInOrder = new List<int>();
for (; index < inOrder.Length; index++)
{
if (inOrder[index] == nodeValue)
break;
leftInOrder.Add(inOrder[index]);
}
// Create In Order Elements for Right Side
List<int> rightInOrder = new List<int>();
for (index = index + 1; index < inOrder.Length; index++)
{
rightInOrderDic.Add(inOrder[index], inOrder[index]);
rightInOrder.Add(inOrder[index]);
}
// Create Pre Order Elements for Right Side
List<int> leftPreOrder = new List<int>();
int start = 1;
for (; start < preOrder.Length; start++)
{
if (rightInOrderDic.ContainsKey(preOrder[start]))
break;
leftPreOrder.Add(preOrder[start]);
}
// Create Pre Order Elments for Left Side
List<int> rightPreOrder = new List<int>();
for (; start < preOrder.Length; start++)
{
rightPreOrder.Add(preOrder[start]);
}
TreeNode newNode = new TreeNode();
newNode.Data = nodeValue;
newNode.Left = FromInOrderAndPreOrder(leftPreOrder.ToArray(), leftInOrder.ToArray());
newNode.Right = FromInOrderAndPreOrder(rightPreOrder.ToArray(), rightInOrder.ToArray());
return newNode;
}