Skip to content

Instantly share code, notes, and snippets.

@haase1020
Last active December 4, 2021 10:49
Show Gist options
  • Save haase1020/8a4bdf909edca976b3a4f1390dfc0916 to your computer and use it in GitHub Desktop.
Save haase1020/8a4bdf909edca976b3a4f1390dfc0916 to your computer and use it in GitHub Desktop.
#101 LeetCode Symmetric Tree Recursive Solution
var isSymmetric = function (root) {
if (!root) return "no tree was provided 🤔"; // return true
return dfs(root.left, root.right);
function dfs(leftNode, rightNode) {
console.log("running this code block"); // this will run at most for the total number of nodes in the tree
if (!leftNode && !rightNode) {
return "you have a perfectly symmetric tree! 🌲"; //return true
}
if (
(leftNode && !rightNode) ||
(!leftNode && rightNode) ||
leftNode.value !== rightNode.value
) {
return "your tree is not symmetric 🌴"; // return false
}
return (
dfs(leftNode.right, rightNode.left) && dfs(leftNode.left, rightNode.right)
);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment