From a999b0f9080f14c05ac40587a5be3a766606e780 Mon Sep 17 00:00:00 2001 From: Abu Asif Khan Chowdhury Date: Mon, 21 Oct 2019 18:12:52 +0200 Subject: [PATCH 1/2] Adding Check if a binary tree is subtree of another binary tree --- .../index.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/_DataStructures_/Trees/BinaryTree/check-binary-tree-subtree-another-binary-tree/index.js diff --git a/src/_DataStructures_/Trees/BinaryTree/check-binary-tree-subtree-another-binary-tree/index.js b/src/_DataStructures_/Trees/BinaryTree/check-binary-tree-subtree-another-binary-tree/index.js new file mode 100644 index 00000000..ce07deb8 --- /dev/null +++ b/src/_DataStructures_/Trees/BinaryTree/check-binary-tree-subtree-another-binary-tree/index.js @@ -0,0 +1,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, +}; \ No newline at end of file From 0f596d864e6d9c93e2fffe6859c6d926923b7576 Mon Sep 17 00:00:00 2001 From: Abu Asif Khan Chowdhury Date: Tue, 22 Oct 2019 10:30:21 +0200 Subject: [PATCH 2/2] Description added and variable fix --- .../index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/_DataStructures_/Trees/BinaryTree/check-binary-tree-subtree-another-binary-tree/index.js b/src/_DataStructures_/Trees/BinaryTree/check-binary-tree-subtree-another-binary-tree/index.js index ce07deb8..0db30df9 100644 --- a/src/_DataStructures_/Trees/BinaryTree/check-binary-tree-subtree-another-binary-tree/index.js +++ b/src/_DataStructures_/Trees/BinaryTree/check-binary-tree-subtree-another-binary-tree/index.js @@ -1,9 +1,15 @@ +// Check if a binary tree is subtree of another binary tree +// Root of the source tree and the tree to be matched are provided in the parameters. +// Return true/false based on whether or not the given tree is the subtree of the source tree +// Time complexity : O(m*n) m is the number of nodes of the original tree, +// n is the number of nodes in the tree to be matched + function areIdentical(rootOfOriginalTree, rootOfMatchingTree) { if (rootOfOriginalTree === null && rootOfMatchingTree === null) { return true; } - if (Roo1 === null || rootOfMatchingTree === null) { + if (rootOfOriginalTree === null || rootOfMatchingTree === null) { return false; }