The TransCompilator

Solved LeetCode program · 106 of 1,000

#0106 · Construct Binary Tree from Inorder and Postorder Traversal

Medium Array chunk-03.md

Original English source

Keep a shared inorder holding 9, 3, 15, 20, and 7. Keep a shared postorder holding 9, 15, 7, 20, and 3. 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 postindex 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 postindex. The root is item current of postorder. Subtract 1 from postindex. 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 1. Attach from split plus 1 with inend. Set parent to slot. Set side to 0. Attach from instart with split minus 1.

Set postindex to 4. 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. Place node into order. Gather from item node of lefts. Gather from item node of rights.

Gather from 0. For each node in order, show item node of values.

Open in Playground Browse all 1,000 problems

Expected stdout

3
9
20
15
7