A maintenance crew mounted two circuit boards onto the same branching bracket lattice. The lattice branches like a binary tree: each mounting point holds at most two child mounting points, a left branch and a right branch. The first board records a unique device ID at every mounting point; the second board, screwed onto that exact same lattice, records a battery percentage at every mounting point instead (battery readings may repeat). Because both boards share the identical lattice shape, the mounting point holding a given device's ID on the first board sits at exactly the same branching position as some mounting point on the second board.
Given a target device ID, find the battery percentage recorded at the corresponding mounting point on the second board.
n — the number of tokens used to describe each board's lattice in level order.n tokens separated by spaces, describing the ID board in level order (breadth-first, top to bottom, left to right within a level): each token is either an integer device ID or the literal null, marking a missing mounting point. Any missing child that is followed by another mounting point in the same or a later level must have its null written explicitly; only mounting points that would trail at the very end of the whole list may be left out (though you may always include them as null too).n tokens separated by spaces, describing the battery board in level order using the exact same layout convention. The battery board's lattice shape is guaranteed to be identical to the ID board's lattice shape, mounting point for mounting point (the same positions are null on both boards).target — a device ID guaranteed to appear exactly once among the ID board's tokens.A single integer: the battery percentage recorded at the mounting point of the battery board that occupies the same branching position as the mounting point holding target on the ID board.
null (each board has at least one mounting point).target equals exactly one non-null token among the ID board's tokens.Example 1
Input
5 1 2 3 null 4 50 20 30 null 70 2
Expected
20
Explanation
The ID lattice built from `1 2 3 null 4` has root ID 1 with left child ID 2 and right child ID 3; ID 2 has no left child but has a right child ID 4. The battery lattice built from `50 20 30 null 70` shares this exact shape: root battery 50 with left child battery 20 and right child battery 30, and that left child's right child is battery 70. Device ID 2 sits at the root's left mounting point, so the corresponding battery reading is the battery board's root-left value, 20.
Example 2
Input
1 9 42 9
Expected
42
Explanation
Both boards have a single mounting point (the root), so the target device ID 9 and its corresponding battery point are both the root. The battery board's only value is 42, so that is the answer.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →