Skip to content
Problem 572 · LeetCode 572(opens in a new tab)

Subtree of Another Tree

EasyRecursion

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 lacks

Example 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.

Try it on LeetCode(opens in a new tab)
  1. Compare at every nodeO(m·n)
  2. 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

ApproachTimeRelative work, worst case: 2,000 and 1,000 equal valuesVerdict
Compare at every nodeO(m·n)
Passes; the usual first answer
Compare only at matching heightsO(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 same when 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

  1. 1Clarify. Does it have to be a whole subtree, down to the leaves? Can values repeat? Does root count as its own subtree?
  2. 2Write same-tree first. Both empty: true. One empty or values differ: false. Otherwise recurse left and right.
  3. 3Give the simple search. Try same at every node of root: O(m·n) in the worst case, which passes here.
  4. 4Cut the work with heights. Only nodes as tall as subRoot can match, and those subtrees never overlap, so the checks total O(m).
  5. 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?