Notice Period

Branch Office Match

easy

A franchise auditor carries the blueprint of a model branch office — a small binary tree. Walking the company's full org tree, she must decide: does any node in the big tree, taken together with all its descendants, match the blueprint exactly?

Given roots main and pattern, return True if some subtree of main is identical (shape and values) to pattern, else False. A subtree includes a node and all of its descendants — no trimming allowed.

Tests pass two level-order arrays; implement the core in _is_subtree on real nodes.

Example 1
in main = [3, 4, 5, 1, 2], pattern = [4, 1, 2]
out True
The subtree rooted at node 4 matches the pattern exactly.
Example 2
in main = [3, 4, 5, 1, 2, null, null, null, null, 0], pattern = [4, 1, 2]
out False
Node 4's subtree now carries an extra 0 below the 2 — full subtrees must match, descendants and all.
Example 3
in main = [1, 1], pattern = [1]
out True
Constraints
  • · 1 <= nodes in main <= 2000
  • · 1 <= nodes in pattern <= 1000
  • · -10^4 <= node value <= 10^4
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.