Trains the technique from
LeetCode 662Maximum Width 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 vine trellis is built in tiers. One plant sits at the top, and every plant may send a shoot down-left and a shoot down-right into the tier below it, so tier 0 has one slot, tier 1 has two slots, tier 2 has four, and so on. Slots with no shoot in them are simply empty.
The trellis arrives as trellis, a tier-by-tier listing. trellis[0] is the top plant's tag. The rest of the list is consumed two entries at a time, taking plants in the order they were listed: the next two entries are that plant's down-left and down-right tags, and null means that shoot is absent, so it contributes no entries of its own. Trailing nulls are omitted.
The width of a tier is the number of slots covered when you go from its leftmost filled slot to its rightmost filled slot inclusive; empty slots that lie between those two ends still count, because the framework holds them whether a shoot arrived or not.
Return the largest width over all tiers.
Example 1
Tier 2 has plant 2 in its first slot and plant 7 in its fourth slot, with the two middle slots empty, so it covers four slots.
Example 2
Tier 3 has plant 6 in its first slot and plant 7 in its eighth slot, so it covers eight slots.
Example 3
Each tier holds a single plant, so no tier covers more than one slot.
Example 4
The trellis is a single plant on tier 0.
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 width_of_binary_tree(trellis: list) -> int:public int widthOfBinaryTree(Integer[] trellis)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.