Skip to content

Instantly share code, notes, and snippets.

@lienista
lienista / practice-precendence-rule.js
Created October 23, 2018 02:11
(Algorithms in Javascript) Practice. A precedence rule is given as "P>E", which means that letter "P" is followed by letter "E". Write a function, given an array of precedence rules, that finds the word represented by the given rules. Note: Each represented word contains a set of unique characters, i.e. the word does not contain duplicate letters.
/*
we create 2 separate arrays of letters and count
the number of characters resulting from the
original precedence array.
we look up the index of each letter from first letter
array and follow the index of the next letter.
*/
function findWord(a){
console.log(a);
@lienista
lienista / ctci-4.9-bst-sequences.js
Created October 18, 2018 22:07
(Algorithms in Javascript) CTCI 4.9. BST Sequences: A BST was created by traversing through an array from left to right and inserting each element. Given a binary search tree with distinct elements, print all possible arrays that could have led to this tree.
// 4.9. BST Sequences: A BST was created by traversing through an array from left to right
// and inserting each element. Given a binary search tree with distinct elements, print all
// possible arrays that could have led to this tree.
import { BinaryTree } from "../helpers/tree.js";
const bstSequences = (root) => {
}
@lienista
lienista / ctci-4.8-first-common-ancestor.js
Last active September 27, 2021 07:16
(Algorithms in Javascript) CTCI 4.8. First Common Ancestor: Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. Note: This is not necessarily a binary search tree.
// 4.8. First Common Ancestor: Design an algorithm and write code to find
// the first common ancestor of two nodes in a binary tree. Avoid storing
// additional nodes in a data structure. Note: This is not necessarily a binary search tree.
import { BinaryTree } from "../helpers/tree.js";
const firstCommonAncestor = (root, p, q) =>{
if(!root)
return null;
//If p contains q, or q contains p
@lienista
lienista / leetcode-285-inorder-successor-in-bst.js
Last active October 11, 2018 01:43
(Algorithms in Javascript) // Leetcode #285. Inorder Successor in BST: Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Note: If the given node has no in-order successor in the tree, return null.
// 4.6. Follow up - Successor: Write an algorithm to find the "next" node (i.e., in-order successor)
// of a given node in a BST. Node do not have a link to its parent.
import { BinaryTree } from "../helpers/tree.js";
const inorderSuccessor = (root, p) => {
if(!p) return null;
//if node has right subtree, get the min of left subtree.
if(p.right) return findMinLeft(p.right);
if(!p.right) return closestAncestorAsLeftChild(root, p)
@lienista
lienista / ctci-4.6-inorder-successor.js
Created October 11, 2018 00:35
(Algorithms in Javascript) CTCI 4.6. Inorder Successor: Write an algorithm to find the "next" node (i.e., in-order successor) of a given node in a BST. You may assume that each node has a link to its parent.
// 4.6. Successor: Write an algorithm to find the "next" node (i.e., in-order successor)
// of a given node in a BST. You may assume that each node has a link to its parent.
import { BinaryTree } from "../helpers/tree.js";
const inOrderSuccessor = (root) => {
if(!root) return null;
//if node has right subtree, get the min of left subtree.
if(root.right) return findMinLeft(root.right);
if(!root.right) return closestAncestorAsLeftChild(root)
@lienista
lienista / tree.js
Created October 10, 2018 06:38
(Algorithms in Javascript) Binary Search Trees
class BinaryTreeNode {
constructor(value){
this.value = value;
this.parent = this.left = this.right = null;
}
}
class BinaryTree {
constructor(value){
this.root = value || null;
@lienista
lienista / ctci-4.5-validate-bst.js
Last active October 10, 2018 06:18
(Algorithms in Javascript) CTCI 4.5. Validate BST: Implement a function to check if a binary tree is a BST.
// 4.5. Validate BST: Implement a function to check if a binary tree is a BST.
import { BinaryTree } from "../helpers/tree.js";
const isValidBST = (root) => {
if(!root) return true;
let check = {
min: Number.NEGATIVE_INFINITY,
max: Number.POSITIVE_INFINITY,
}
return validateBST(root, check);
@lienista
lienista / ctci-4.4-check-balanced.js
Last active October 10, 2018 07:18
(Algorithms in Javascript) CTCI 4.4. Check balanced: Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.
// 4.4 Check balanced: Implement a function to check if a binary tree is balanced.
// For the purposes of this question, a balanced tree is defined to be a tree such
// that the heights of the two subtrees of any node never differ by more than one.
class BinaryTreeNode {
constructor(value){
this.value = value;
this.parent = this.left = this.right = null;
}
}
@lienista
lienista / ctci-4.3-list-of-depths.js
Last active September 27, 2018 00:00
(Algorithms in Javascript) 4.3. List of Depths: Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you'll need D linked lists).
// 4.3. List of Depths: Given a binary tree, design an algorithm which creates a linked
// list of all the nodes at each depth
// (e.g., if you have a tree with depth D, you'll need D linked lists).
class LinkedListNode(data) {
constructor(data, next){
this.data = data || null;
this.next = next || null;
}
}
@lienista
lienista / ctci-4.2-minimal-tree.js
Last active September 27, 2018 22:51
(Algorithms in Javascript) CTCI 4.2. Minimal Tree: Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height.
// 4.2. Minimal Tree:
// Minimal Tree: Given a sorted (increasing order) array with unique integer elements,
// write an algorithm to create a binary search tree with minimal height.
class BinaryTreeNode {
constructor(value){
this.value = value;
this.parent = this.left = this.right = null;
}
}