Last active
December 4, 2021 10:49
-
-
Save haase1020/8a4bdf909edca976b3a4f1390dfc0916 to your computer and use it in GitHub Desktop.
#101 LeetCode Symmetric Tree Recursive Solution
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
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