Created
December 6, 2021 10:57
-
-
Save haase1020/0748646b3053dfba615d424fd09c0d2f to your computer and use it in GitHub Desktop.
same tree leetcode #100
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
// recursive solution: time complexity: O(n) | space complexity: O(h) where h is height of tree (recursion stack) | |
var isSameTree = function (p, q) { | |
if (!p && !q) { | |
return true; | |
} | |
if (!p || !q || p.val !== q.val) { | |
return false; | |
} | |
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment