Skip to content

Instantly share code, notes, and snippets.

@adrianosferreira
Created August 20, 2019 13:09
Show Gist options
  • Save adrianosferreira/094728fb95a5c8d9c418193b6174dfdf to your computer and use it in GitHub Desktop.
Save adrianosferreira/094728fb95a5c8d9c418193b6174dfdf to your computer and use it in GitHub Desktop.
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