Created
December 15, 2020 16:35
-
-
Save iJustErikk/adf1c3a00a54ff11c4d50b39b6d082a3 to your computer and use it in GitHub Desktop.
This file contains 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
// recursive function wrapper | |
function isTreeSymmetric(t) { | |
// empty tree is automatically symmetric | |
if (!t) return true; | |
return isTreeSymmetricRecursive(t.left, t.right); | |
} | |
function isTreeSymmetricRecursive(left, right) { | |
// implementation detail | |
// if we dont have children, we are symmetric ourselves | |
if (!(left || right)) return true; | |
// if we only have one child, we cannot be symmetric | |
if (!left || !right) return false; | |
return (left.value === right.value) | |
&& isTreeSymmetricRecursive(left.left, right.right) | |
&& isTreeSymmetricRecursive(left.right, right.left); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment