Skip to content

Instantly share code, notes, and snippets.

@iJustErikk
Created December 15, 2020 16:35
Show Gist options
  • Save iJustErikk/adf1c3a00a54ff11c4d50b39b6d082a3 to your computer and use it in GitHub Desktop.
Save iJustErikk/adf1c3a00a54ff11c4d50b39b6d082a3 to your computer and use it in GitHub Desktop.
// 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