Solved LeetCode program · 105 of 1,000
#0105 · Construct Binary Tree from Preorder and Inorder Traversal
Original English source
Keep a shared preorder holding 3, 9, 20, 15, and 7. Keep a shared inorder holding 9, 3, 15, 20, and 7. Keep a shared values as an empty list of whole numbers. Keep a shared lefts as an empty list of whole numbers. Keep a shared rights as an empty list of whole numbers. Keep a shared preindex of 0. Keep a shared index of 0. Keep a shared parent of -1. Keep a shared side of 0.
To attach from instart (a whole number) with inend (a whole number). If instart is more than inend, return. The current is preindex. The root is item current of preorder. Add 1 to preindex. The slot is index. Add 1 to index. Place root into values. Place -1 into lefts. Place -1 into rights. The anchor is parent. If anchor is at least 0 and side is 0, set item anchor of lefts to slot. If anchor is at least 0 and side is 1, set item anchor of rights to slot. The split is instart. For every spot from instart to inend, if item spot of inorder is root, set split to spot. Set parent to slot. Set side to 0. Attach from instart with split minus 1. Set parent to slot. Set side to 1. Attach from split plus 1 with inend.
Attach from 0 with 4. Keep a shared order as an empty list of whole numbers.
To gather from node (a whole number). If node is less than 0, return. Gather from item node of lefts. Gather from item node of rights. Place node into order.
Gather from 0. For each node in order, show item node of values.
Expected stdout
9 15 7 20 3