Trains the technique from
LeetCode 572Subtree of Another 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.
Two cargo rigs are given as the flat lists rig and section. Each hangs from a single top joint, every joint carries at most two joints below it in a first and a second slot, and either slot may be empty.
A flat list is read level by level: its first entry is the top joint's load, and reading left to right, every entry that is not null claims the next two unused positions as its first and second slot in that order, while null marks an empty slot and claims no positions of its own.
Return whether some joint of rig, taken together with everything hanging below it, matches section exactly: the same shape, slot for slot, and the same loads throughout.
Example 1
The joint loaded 2 carries 4 in its first slot and 5 in its second, which is exactly the section asked about.
Example 2
The section is a single bare joint loaded 1, but the rig's only joint loaded 1 carries another joint below it, so the shapes differ.
Example 3
The whole rig is itself a single bare joint matching the section.
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 is_subtree(rig: list, section: list) -> bool:public boolean isSubtree(Integer[] rig, Integer[] section)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.