Skip to content

Commit a999b0f

Browse files
committed
Adding Check if a binary tree is subtree of another binary tree
1 parent 43ed1bc commit a999b0f

File tree

1 file changed

+31
-0
lines changed
  • src/_DataStructures_/Trees/BinaryTree/check-binary-tree-subtree-another-binary-tree

1 file changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function areIdentical(rootOfOriginalTree, rootOfMatchingTree) {
2+
if (rootOfOriginalTree === null && rootOfMatchingTree === null) {
3+
return true;
4+
}
5+
6+
if (Roo1 === null || rootOfMatchingTree === null) {
7+
return false;
8+
}
9+
10+
return (rootOfOriginalTree.value === rootOfMatchingTree.value) && areIdentical(rootOfOriginalTree.leftChild, rootOfMatchingTree.leftChild) && areIdentical(rootOfOriginalTree.rightChild, rootOfMatchingTree.rightChild);
11+
}
12+
13+
function isSubtree(rootOfOriginalTree, rootOfMatchingTree) {
14+
if (rootOfMatchingTree === null) {
15+
return true;
16+
}
17+
18+
if (rootOfOriginalTree === null) {
19+
return false;
20+
}
21+
22+
if (areIdentical(rootOfOriginalTree, rootOfMatchingTree)) {
23+
return true;
24+
}
25+
26+
return isSubtree(rootOfOriginalTree.leftChild, rootOfMatchingTree) || isSubtree(rootOfMatchingTree.rightChild, rootOfMatchingTree);
27+
}
28+
29+
module.exports = {
30+
isSubtree,
31+
};

0 commit comments

Comments
 (0)