Created
August 20, 2019 13:09
-
-
Save adrianosferreira/094728fb95a5c8d9c418193b6174dfdf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isProperSubTree( b1, b2, found = false ) { | |
if ( found ) { | |
return found; | |
} | |
if( b2.value === b1.value && b2.left === b1.left && b2.right === b1.right ) { | |
found = true; | |
} | |
if(typeof b1.right === 'object') { | |
found = isProperSubTree( b1.right, b2, found ); | |
} | |
if(typeof b1.left === 'object') { | |
found = isProperSubTree( b1.left, b2, found ); | |
} | |
return found; | |
} | |
let b1 = { | |
value: 4, | |
left: { | |
value: 2, | |
left: { | |
value: 3, | |
left: 4, | |
right: { | |
value: 2, | |
left: { | |
value: 3, | |
left: { | |
value: 3, | |
left: { | |
value: 3, | |
left: { | |
value: 100, | |
left: 101, | |
right: { | |
value: 100, | |
left: 101, | |
right: { | |
value: 100, | |
left: 101, | |
right: { | |
value: 100, | |
left: 101, | |
right: { | |
value: 100, | |
left: { | |
value: 100, | |
left: { | |
value: 100, | |
left: { | |
value: 100, | |
left: 101, | |
right: { | |
value: 200, | |
left: 201, | |
right: 202 | |
}, | |
}, | |
right: 102 | |
}, | |
right: 102 | |
}, | |
right: 102 | |
}, | |
}, | |
}, | |
}, | |
}, | |
right: 5 | |
}, | |
right: 5 | |
}, | |
right: 5 | |
}, | |
right: { | |
value: 2, | |
left: { | |
value: 80, | |
left: 81, | |
right: 82 | |
}, | |
right: 3 | |
} | |
} | |
}, | |
right: 3 | |
}, | |
right: { | |
value: 2, | |
left: { | |
value: 8, | |
left: 7, | |
right: 9 | |
}, | |
right: 3 | |
} | |
}; | |
let b2 = { | |
value: 200, | |
left: 201, | |
right: 202 | |
}; | |
console.log(isProperSubTree( b1, b2 )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment