This file contains 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; | |
} |
This file contains 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 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 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
// https://leetcode.com/problems/binary-tree-inorder-traversal/ | |
const dft = (root, result) => { | |
if (!root) return; | |
dft(root.left, result); | |
result.push(root.val); | |
dft(root.right, result); | |
return result; | |
}; |
This file contains 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
// multiple recursion: one than one recursive call per iteration | |
// time complexity: O(2^n) or exponential time | |
// space complexity: O(n) | |
function fibonacci(n) { | |
if ((n === 1) | (n === 2)) return 1; | |
return fibonacci(n - 1) + fibonacci(n - 2); // multiple calls of fibonacci | |
} | |
// better but still slower than iterative | |
function fibonacciMemoized(n, cache = []) { |
This file contains 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
function factorial(n) { | |
let answer = 1; | |
if (n == 0 || n == 1) { | |
return answer; | |
} else { | |
for (var i = n; i >= 1; i--) { | |
console.log("running"); // should log 5 times | |
answer = answer * i; | |
} | |
return answer; |
This file contains 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
function factorial(n) { | |
if (n == 0 || n == 1) { | |
return 1; | |
} else { | |
console.log("running"); // should run 5 times | |
return n * factorial(n - 1); | |
} | |
} | |
console.log(factorial(5)); //5*4*3*2*1 = 120 |
This file contains 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
const todoList = [ | |
"pick up the floor", | |
"clear the table", | |
"put away dishes", | |
"sweep", | |
"clean the bathrooms", | |
"dust", | |
"mop", | |
]; |
This file contains 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
const todoList = [ | |
"pick up the floor", | |
"clear the table", | |
"put away dishes", | |
"sweep", | |
"clean the bathrooms", | |
"dust", | |
"mop", | |
]; |
This file contains 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 ( |
NewerOlder