-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
31 lines (27 loc) · 888 Bytes
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class TreeNode {
constructor(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}
*/
const isSubtree = (root, subRoot) => {
let queue = [root];
const isNodeSame = (node, sub) => {
if ((node?.val ?? null) !== (sub?.val ?? null)) return false;
if (null === node && null === sub) return true;
return isNodeSame(node?.left ?? null, sub?.left ?? null) && isNodeSame(node?.right ?? null, sub?.right ?? null);
}
while (queue.length > 0) {
let curr = queue.shift(), left = curr.left, right = curr.right;
if (isNodeSame(curr, subRoot)) return true;
if (null !== left) queue.push(left);
if (null !== right) queue.push(right);
}
return false;
};