Code coverage report for lib/algorithms/4-binaryTrees/isSubtree.js

Statements: 93.33% (14 / 15)      Branches: 95.24% (20 / 21)      Functions: 100% (2 / 2)      Lines: 100% (11 / 11)      Ignored: none     

All files » lib/algorithms/4-binaryTrees/ » isSubtree.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21    1 59 31   31 3   28   1 22   12   11       1
//4.8 Given two binary trees, determine if one tree is a subtree of the other
 
var isSubtree = function(node, tree) {
    if(node === null) return false;
    Iif(tree === null) return true;
 
    if((node.data === tree.head.data) && isMatch(node, tree.head))
        return true;
    else
        return isSubtree(node.left, tree) || isSubtree(node.right, tree);
 
    function isMatch(node, subNode) {
        if((node === null) && (subNode === null)) return true;
 
        if((node === null) || (subNode === null)) return false; //values are different
 
        return (node.data === subNode.data) && isMatch(node.left, subNode.left) && isMatch(node.right, subNode.right);
    }
};
 
module.exports = isSubtree;