FreeCareerPath
Build the PrimitiveEasy

Subtree of Another Tree

Write `is_subtree(root, sub)` returning `True` when `sub` appears as a subtree of `root` — matching a node and *all* of its descendants exactly. This is two problems: an exact same-tree comparison, and trying it at every node. Say the complexity out loud: O(n * m). An empty `sub` is a subtree of anything, and a partial match is not a match — those two cases are where wrong answers show up.

What to expect: A timer starts when you begin. Edit the starter code, run it against the test suite as many times as you like, then finish when you're done. The reference solution and interviewer follow-up questions unlock only after you finish.

Log in to start this session

Timed sessions and your results for "Subtree of Another Tree" are saved to your account — logging in takes a few seconds.

Subtree of Another Tree — FreeCareerPath