Trains the technique from
LeetCode 105Construct Binary Tree from Preorder and Inorder TraversalThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A caving club has surveyed a dry cave system. Each chamber carries its own depth marker, no two markers alike, and from any chamber at most two passages lead onward: a near one and a far one. A single chamber serves as the entrance.
Two teams walked the system, and both always took a chamber's near passage before its far passage.
entry_order.wall_order.Because the markers are all different, exactly one layout produces both logs. Rebuild it and hand it back level by level, beginning with the entrance. Every chamber that exists puts two slots on the level below it, near slot before far slot, holding either the marker of the chamber found there or null when that passage does not exist. Any null slots trailing after the final marker are left off the answer.
Example 1
Marker 10 is the entrance, and the wall log puts -8, 4 and 6 on its near side, so 4 heads that branch with -8 and 6 hanging off it. On the far side 17 has no near passage, and its far passage leads to 25, whose own near passage reaches 20.
Example 2
Every chamber sits on the near side of the entrance, so 1 has no far passage at all, and the branch below alternates sides on the way down.
Example 3
A system of one chamber has no passages, so the answer holds just the entrance marker with no trailing null slots.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def rebuild_cave(entry_order: list[int], wall_order: list[int]) -> list:public Integer[] rebuildCave(int[] entryOrder, int[] wallOrder)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.