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
| // Bind returns function closure for later usage, | |
| Function.prototype.myBind = function (context) { | |
| const fn = this; | |
| return function () { | |
| fn.apply(context, arguments); | |
| } | |
| } | |
| // Apply, bind invokes the function immediately. |
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
| const assert = require("assert"); | |
| // O(n) time | O(h) space - h is height. | |
| function nodeDepths(root, depth = 0) { | |
| if (root === null) { | |
| return 0; | |
| } | |
| return depth + nodeDepths(root.left, depth + 1) + nodeDepths(root.right, depth + 1); | |
| } |
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
| // O(n) time & space | |
| function invertBinaryTree(tree) { | |
| if (tree === null) return; | |
| swapLeftAndRight(tree); | |
| invertBinaryTree(tree.left); | |
| invertBinaryTree(tree.right); | |
| } | |
| function swapLeftAndRight(tree) { | |
| const left = tree.left; |
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
| const assert = require("assert"); | |
| function BST(value) { | |
| this.value = value; | |
| this.left = null; | |
| this.right = null; | |
| } | |
| function findKthLargestValueInBst(tree, k) { | |
| const result = []; |
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
| const assert = require('assert'); | |
| // Finding successor means, traverse the tree by inorder and the selected node's previous node is successor. | |
| function findSuccessor(root, node) { | |
| // Right child exists, move to the left most child for right branch, | |
| // If the right child does not exists, successor will be rigth most parent, | |
| if (node.right !== null) return getLeftmostChild(node.right); | |
| return getRightmostParent(node); | |
| } |
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
| const assert = require('assert'); | |
| // Write function that takes in a non-empty sorted array of distinct integers, | |
| // constructs a BST from the integers, and returns the root of the BST. | |
| function minHeightBst(array) { | |
| // Find the median of array, | |
| return constructMinHeightBST(array, 0, array.length - 1) | |
| } | |
| function constructMinHeightBST(array, startIndex, endIndex) { |
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
| const assert = require("assert"); | |
| function BST(value) { | |
| this.value = value; | |
| this.left = null; | |
| this.right = null; | |
| } | |
| // Left, root, right - root is mid | |
| function inOrderTraverse(tree, array) { |
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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
| <title>Auto complete</title> | |
| <style> | |
| .auto-complete { | |
| width: 300px; |
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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
| <title>Accordion</title> | |
| <style> | |
| .accordion-wrapper { | |
| display: flex; |
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
| class Node { | |
| constructor(name) { | |
| this.name = name; | |
| this.children = []; | |
| } | |
| addChild(name) { | |
| this.children.push(new Node(name)); | |
| return this; | |
| } |