forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (24 loc) · 935 Bytes
/
index.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
function areIdentical(rootOfOriginalTree, rootOfMatchingTree) {
if (rootOfOriginalTree === null && rootOfMatchingTree === null) {
return true;
}
if (Roo1 === null || rootOfMatchingTree === null) {
return false;
}
return (rootOfOriginalTree.value === rootOfMatchingTree.value) && areIdentical(rootOfOriginalTree.leftChild, rootOfMatchingTree.leftChild) && areIdentical(rootOfOriginalTree.rightChild, rootOfMatchingTree.rightChild);
}
function isSubtree(rootOfOriginalTree, rootOfMatchingTree) {
if (rootOfMatchingTree === null) {
return true;
}
if (rootOfOriginalTree === null) {
return false;
}
if (areIdentical(rootOfOriginalTree, rootOfMatchingTree)) {
return true;
}
return isSubtree(rootOfOriginalTree.leftChild, rootOfMatchingTree) || isSubtree(rootOfMatchingTree.rightChild, rootOfMatchingTree);
}
module.exports = {
isSubtree,
};