/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} subRoot
* @return {boolean}
*/
var isSubtree = function(root, subRoot) {
const q = [root];
while(q.length > 0) {
const curr = q.shift();
if (curr === null) continue;
if (curr.val === subRoot.val) {
if (validate(curr, subRoot)) return true;
}
curr.left !== null && q.push(curr.left);
curr.right !== null && q.push(curr.right);
}
return false;
};
function validate(node1, node2) {
if (node1 === null && node2 === null) return true;
if (node1 === null || node2 === null) return false;
if (node1.val !== node2.val) return false;
return validate(node1.left, node2.left) && validate(node1.right, node2.right);
}