Rebuild a tree from two walks
transcript
Keep two walks of a tree. The preorder and the inorder. No marks are needed. The root splits the rest.
The first preorder value is eight. In the inorder, it sits at place three. Three values are before it. Two values are after it.
Cut the preorder the same way. The next three values go left. The rest go right. An empty part needs no mark.
Searching for the root costs a scan. On a long chain, that is five billion steps. Build a map from value to place. Each lookup is one step.
With postorder, the root comes last. The inorder splits the parts the same way. A search tree needs only its preorder. Its order gives the marks.
Each value is placed once. Each lookup in the map is one step. So the rebuild costs n steps. Pass places, not copies of lists.
The preorder gives the root. The inorder gives the parts. A map makes it quick. Full breakdown in the lecture.