You are given two binary trees, root and subRoot. Decide whether root contains a node whose subtree is an exact copy of subRoot: the same shape, with the same value in every position.
A node’s subtree is that node together with everything below it. Being a copy is all or nothing: the copy must include every descendant of that node, so an extra child anywhere breaks it. root itself counts too, so a tree is a subtree of itself.
Example 1
Input: root = [6, 2, 9, 1, 5], subRoot = [2, 1, 5] Output: true Why: node 2 with its children 1 and 5 is a copy
2 more examples and the constraints
Example 2
Input: root = [6, 2, 9, 1, 5, null, null, null, 7],
subRoot = [2, 1, 5]
Output: false
Why: under node 2, the 1 has a child 7 that subRoot lacksExample 3
Input: root = [4, 4], subRoot = [4] Output: true Why: the leaf 4 matches; the root 4 has a child, so it does not
Constraints
- 1 ≤ nodes in root ≤ 2,000
- 1 ≤ nodes in subRoot ≤ 1,000
- Every value is between −10,000 and 10,000
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Compare at every nodeO(m·n)
- Compare only at matching heightsO(m + n)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work, worst case: 2,000 and 1,000 equal values | Verdict |
|---|---|---|---|
| Compare at every node | O(m·n) | Passes; the usual first answer | |
| Compare only at matching heights | O(m + n) | Best; linear in both trees |
m = nodes in root, n = nodes in subRoot, h = height of the tree.
Edge cases to test
- Extra nodes below the match. Example 2: a matching top is not enough; every descendant must line up, down to the empty children.
- The whole tree. [1, 2, 3] is a subtree of [1, 2, 3].
- subRoot bigger than root. [1] cannot contain [1, 2]: the answer is false.
- Same values, different shape. [1, 2] and [1, null, 2] hold the same numbers, but 2 is a left child in one and a right child in the other.
- Repeated values. Many nodes can share the root value of subRoot. The first one that matches by value may still fail.
Common mistakes
- Checking only values in some order, such as an in-order list, which can match two different shapes.
- Stopping
samewhen one side runs out instead of requiring both to be empty together. - Returning the result of the first node whose value matches, instead of trying the others when it fails.
How to talk through it
- 1Clarify. Does it have to be a whole subtree, down to the leaves? Can values repeat? Does root count as its own subtree?
- 2Write same-tree first. Both empty: true. One empty or values differ: false. Otherwise recurse left and right.
- 3Give the simple search. Try
sameat every node of root: O(m·n) in the worst case, which passes here. - 4Cut the work with heights. Only nodes as tall as subRoot can match, and those subtrees never overlap, so the checks total O(m).
- 5Test out loud. Run the extra-child case and the mirrored shape before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?