You are given two binary trees through their root references root and subRoot. Your task is to decide whether the tree rooted at root contains a subtree that perfectly matches the tree rooted at subRoot in both structure and node values. If a match is found, return true; otherwise return false.
A subtree of a binary tree is defined by taking any node and all of its descendants. That is, the chosen node together with its complete branch down to every leaf must be considered; you may not leave out any part of that branch. Furthermore, any tree is considered a subtree of itself.
The input for each tree is presented as an array derived from a level-order traversal. Node values are given as integers, and the absence of a left or right child is indicated by the keyword null.
Example 1:
Input: root = [7,4,9,1,5], subRoot = [4,1,5]
Output: true
Explanation: The subtree rooted at the node with value 4, which has left child 1 and right child 5, is structurally and value-wise identical to subRoot. Therefore, true is returned.
root = [7,4, 9, 1, 5] subRoot = [4, 1, 5]
true
We are given two binary trees: `root` with values [7,4,9,1,5] and `subRoot` with values [4,1,5]. We need to check if `subRoot` appears as a complete subtree within `root`.
Example 2:
Input: root = [7,4,9,1,5,null,null,null,null,2], subRoot = [4,1,5]
Output: false
Explanation: In this root, the node 4 still has children 1 and 5, but the node 5 now has an extra left child 2. Because a full subtree must include all descendants, this discrepancy means the branch does not match subRoot. The result is false.
Example 3:
Input: root = [1,1], subRoot = [1]
Output: true
Explanation: The second element of root is a leaf node holding the value 1. This single-node subtree is exactly the same as subRoot, so the answer is true.
Constraints:
root contains between 1 and 2000 nodes.subRoot contains between 1 and 1000 nodes.-10^4 <= Node.val <= 10^4root = [7,4, 9, 1, 5] subRoot = [4, 1, 5]
true
We are given two binary trees: `root` with values [7,4,9,1,5] and `subRoot` with values [4,1,5]. We need to check if `subRoot` appears as a complete subtree within `root`.