Subtree of Another Tree
Given the roots of two binary trees root and subRoot, return true if there is a node in root such that the subtree rooted at that node is identical in structure and values to subRoot; otherwise return false.
Open official problem prompt ↗Decide whether the smaller tree appears verbatim as a complete branch of the larger tree.
Scanning a family tree for a person whose entire line of descendants exactly matches a given smaller family tree, generation for generation.
- Input
- root = [3,4,5,1,2], subRoot = [4,1,2]
- Output
- true
- Why
- The subtree rooted at root's left child (4 with children 1 and 2) exactly matches subRoot.
The number of nodes in root is in the range [1, 2000]The number of nodes in subRoot is in the range [1, 1000]-10^4 <= Node.val <= 10^4