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
// Iterative solution: time complexity: O(n) | space complexity: O(n) | |
var isSameTree = function (p, q) { | |
const q1 = []; | |
q1.push(p); | |
const q2 = []; | |
q2.push(q); | |
while (q1.length && q2.length) { | |
const curr1 = q1.shift(); | |
const curr2 = q2.shift(); |
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); | |
}; |
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
enum SIZE { | |
small = 'small', | |
medium = 'medium', | |
large = 'large' | |
} | |
function calculateAge(operation: Function, initialVal: number, factor: boolean | SIZE) { | |
const age = operation(initialVal, factor) | |
return age; | |
} |
OlderNewer