Subtree of Another Tree
Given the roots of two binary trees root and subRoot, return true if there is a node in root whose subtree is structurally identical to subRoot, including matching values.
Intuition
The trap is checking only whether values match somewhere in the tree, or stopping the search the moment you find a node whose value equals subRoot's root value. A value match by itself proves nothing about structure -- the subtree rooted there could easily branch differently or have different values further down, and it's tempting to treat that first value hit as decisive when it's really just a candidate. You need a full structural equality check, the same one used for 'Same Tree', run at every node of root as a candidate root: values must match at every corresponding position and the shapes must line up exactly, with both trees running out of nodes at the same spots. Just as important, a candidate that fails deep down does not disqualify the real match sitting somewhere else in the tree -- keep walking every node of root and trying the full comparison fresh each time, short-circuiting only once a genuine full match is found.
Approach
Write isSameTree as a helper
Build a standalone structural-equality check: two null nodes are equal, one null and one non-null are not, two non-null nodes are equal only if their values match and both their left and right subtrees are recursively equal.
Try every node of root as a candidate
Walk root with a DFS. At each node, call isSameTree(node, subRoot). If it returns true, the whole answer is true and you can stop. If it returns false, that just rules out this one candidate -- move on to the node's children and keep trying.
Don't let a value match short-circuit the search
Never treat 'the values are equal' alone as a signal to stop; only a true result from the full structural comparison ends the search. A failed comparison at one node is simply discarded, and the walk continues into both children looking for a better candidate.
Solution & live demo
Edge cases
An empty tree is conventionally considered a subtree of anything (isSameTree against null succeeds trivially at any null candidate), though most test suites don't exercise this directly -- follow the same isSameTree logic regardless.
No candidate nodes exist to try, so the walk finds nothing and returns false.
isSameTree catches this correctly since it compares structure, not just values -- a matching root value with mismatched children still returns false for that candidate.
The walk must continue past failed candidates near the top and keep checking every descendant node, since the true match may be far from the root.