Tree construction from Inorder & Preorder [ Microsoft ]

Problem: Provided Inorder and PreOrder of a tree. Construct the tree and print it in PostOrder traversal.

The problem was provided as a functional problem in Microsoft interview as hereĀ  [Courtesy : geeksforgeeks] Solution:

 

10986

Single source shortest path algorithm and priority_queue used to solve the problem.

 

10336

 

112

Tree Summing.

 

521

 

794

This one took me a looong time to figure out the solution, i used dynamic programming approach to modify the bfs traversal.

 

710

This is quite an easy problem. But it took me long time to realize that to determine the number of total game segments is nothing but counting the direction changes to reach the destination.

 

633

 

627

There is another solution using Linked List in case Router ID is not limited from 1 to 300. But for this problem this solution gets TLE.

   

599