The problem. Given the roots of two binary trees, root and subRoot, return true if some node of root has a subtree that is exactly the same as subRoot — the same structure and values, all the way down to the leaves.
root = [3, 4, 5, 1, 2], subRoot = [4, 1, 2] -> true
root = [3, 4, 5, 1, 2, null, null, null, null, 0], subRoot = [4, 1, 2] -> false"Exactly" matters: in the second example, the subtree under 4 has an extra 0 below 2, so it only contains subRoot's nodes — it isn't the same tree. A match must also stop where subRoot stops.
Go through every node of root and ask the Same Tree question: does the tree starting here equal subRoot?
function isSubtree(root, subRoot) { if (root === null) return false; if (isSameTree(root, subRoot)) return true; // does the tree starting here match? return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);} function isSameTree(p, q) { if (p === null && q === null) return true; if (p === null || q === null || p.val !== q.val) return false; return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);}Time O(m · n) for m and n nodes — each of root's nodes may start a comparison that runs deep into subRoot — space O(h). Short and usually fast in practice, but trees full of equal values (all 1s, say) push it to the worst case.
Write each tree as a preorder sequence with a marker for every missing child. The markers pin down the shape exactly, and a subtree's preorder is a contiguous run of the whole tree's preorder. So: subRoot is a subtree of root exactly when subRoot's sequence appears inside root's sequence.
Two details keep it correct. The # markers are essential — without them, [4, 1, 2] and [4, 1, null, 2] serialize identically. And each value needs a separator so that 2 can't match the end of 12.
Watch both sequences being written, then where one appears inside the other.
Space O(m + n) for the two strings. Serializing is O(m + n); the built-in substring search is usually fast, but most standard libraries don't promise better than O(m · n) in the worst case.
To guarantee linear time, search with the Knuth–Morris–Pratt algorithm. First it builds a fail table for the pattern: for each position, the length of the longest prefix of the pattern that also ends there. While scanning, a mismatch doesn't restart the search — it falls back in the pattern using that table, so no character of the text is ever compared twice from scratch. Comparing whole tokens instead of characters also removes the need for separators.
function isSubtree(root, subRoot) { return kmpContains(serialize(root, []), serialize(subRoot, []));} function serialize(node, out) { if (node === null) { out.push("#"); return out; } out.push(String(node.val)); serialize(node.left, out); serialize(node.right, out); return out;} function kmpContains(text, pattern) { // fail[i]: longest proper prefix of pattern[0..i] that is also a suffix of it const fail = new Array(pattern.length).fill(0); for (let i = 1, k = 0; i < pattern.length; i++) { while (k > 0 && pattern[i] !== pattern[k]) k = fail[k - 1]; if (pattern[i] === pattern[k]) k++; fail[i] = k; } for (let i = 0, k = 0; i < text.length; i++) { while (k > 0 && text[i] !== pattern[k]) k = fail[k - 1]; // fall back, never rescan if (text[i] === pattern[k]) k++; if (k === pattern.length) return true; } return false;}Time O(m + n), space O(m + n). The serialization turns a tree question into a string-matching question, and KMP answers that in one pass. (Another linear-time route is tree hashing: give every subtree a hash built from its children's hashes, then compare hashes.)
O(m · n)Each of root's m nodes may start a comparison that runs through subRoot's n nodes.
O(h)Recursion as deep as the tree.
[1, 2, 3] and [3]: true.root = [12], subRoot = [2]: false (this is what the separators prevent).root all 1s with subRoot all 1s but one leaf different: the brute force's worst case.O(m · n)Serializing is O(m + n), but library substring search isn't promised better than O(m · n).
O(m + n)The two strings.
O(m + n)Serialize both, then KMP finds the match in one pass, never moving backwards.
O(m + n)The two strings and KMP's prefix table.