Trains the technique from
LeetCode 543Diameter of Binary TreeThis 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 gallery branches outward from its atrium. Every room has at most two doorways leading further out, and each room carries one signed humidity reading.
The floor plan arrives as rooms, which lists the gallery one level at a time, left to right. Position 0 holds the atrium. Each room named in the listing claims the next two free positions for its outward doorways, the left doorway first and the right one second. A position holding null means no room hangs there, and it claims no positions of its own. Trailing null positions may be left off the listing.
A walk starts in some room, passes through doorways, and never enters a room twice. Its span is the number of doorways it passes through. The walk may start and end anywhere and does not have to reach the atrium.
Return the largest span any walk in the gallery can reach. A gallery with a lone atrium has a largest span of 0. The humidity readings never affect the answer.
Example 1
Walking from the room at position 3 out through position 1, the atrium, and on to position 2 passes through 3 doorways, and nothing longer exists.
Example 2
The gallery leans to one side. The widest walk runs position 5, position 3, position 1, position 4, position 8 for 4 doorways, and it never enters the atrium; any walk that does turn around at the atrium passes through only 3.
Example 3
The atrium stands alone, so every walk passes through no doorways at all.
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 widest_gallery_walk(rooms: list[int | None]) -> int:public int widestGalleryWalk(Integer[] rooms)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.