Skip to content

Instantly share code, notes, and snippets.

@optimistiks
optimistiks / next-greater-element.js
Created January 25, 2024 18:58
Given the two distinct integer arrays, nums1 and nums2, where nums1 is a subset of nums2, find all the next greater elements for nums1 values in the corresponding places of nums2.
function nextGreaterElement(nums1, nums2){
const ans = []
// monotonic stack
const stack = []
const map = {}
nums2.forEach(num => {
if (stack.length === 0) {
@optimistiks
optimistiks / fraction-to-recurring.js
Created January 23, 2024 19:58
Given the two integer values of a fraction, numerator and denominator, implement a function that returns the fraction in string format. If the fractional part repeats, enclose the repeating part in parentheses.
export function fractionToDecimal(numerator, denominator){
if (numerator === 0) {
return 0
}
let result = ""
if (numerator < 0 ^ denominator < 0) {
result = "-";
numerator = Math.abs(numerator)
@optimistiks
optimistiks / populate-next-pointers.js
Created December 21, 2023 21:39
Given a perfect binary tree, where each node contains an additional pointer called next. This pointer is initially set to NULL for all nodes. Your task is to connect all nodes of the same hierarchical level by setting the next pointer to its immediate right node. The next pointer of the rightmost node at each level is set to NULL.
function populateNextPointers(root) {
// we traverse the tree level by level by using two variables,
// left and current
// left is always the leftmost element of the current level,
// current starts the same as left, but moves right along the level
// as soon as current has nowhere to move, we shift level by shifting left down one level, and setting current to the same as left
// so the algorithm is
// root is left, current
@optimistiks
optimistiks / binary-tree-zigzag-order.js
Created December 19, 2023 19:28
Given a binary tree, return its zigzag level order traversal. The zigzag level order traversal corresponds to traversing nodes from left to right for one level, and then right to left for the next level, and so on, reversing direction after every level.
import { TreeNode } from "./ds_v1/BinaryTree.js";
// Definition of a binary tree node
// class TreeNode {
// constructor(data) {
// this.data = data;
// this.left = null;
// this.right = null;
// }
// }
@optimistiks
optimistiks / level-order-traversal.js
Created December 17, 2023 17:25
Given the root of a binary tree, display the values of its nodes while performing a level order traversal. Return the node values for all levels in a string separated by the character :. If the tree is empty, i.e., the number of nodes is 0 0 , then return “None” as the output.
// Definition of a binary tree node
// class TreeNode {
// constructor(data) {
// this.data = data;
// this.left = null;
// this.right = null;
// }
// }
import { TreeNode } from "./ds_v1/BinaryTree.js";
@optimistiks
optimistiks / lowest-common-ancestor.js
Created December 13, 2023 22:06
Given the root node of a binary tree with n nodes, your task is to find the lowest common ancestor of two of its nodes, p and q.
// Definition of a binary tree node
// class TreeNode {
// constructor(data) {
// this.data = data;
// this.left = null;
// this.right = null;
// }
// }
import { TreeNode } from "./ds_v1/BinaryTree.js";
@optimistiks
optimistiks / binary-tree-right-side.js
Created December 12, 2023 19:37
You are given a root of a binary tree that has n number of nodes. You have to return the right-side view in the form of a list. A right-side view of a binary tree is the data of the nodes that are visible when the tree is viewed from the right side.
import { TreeNode } from "./ds_v1/BinaryTree.js";
// Definition of a binary tree node
// class TreeNode {
// constructor(data) {
// this.data = data;
// this.left = null;
// this.right = null;
// }
// }
@optimistiks
optimistiks / binary-tree-from-preorder-inorder.js
Last active December 12, 2023 19:06
Create a binary tree from two integer arrays, pOrder and iOrder, where pOrder represents a preorder traversal of a binary tree, and iOrder represents an inorder traversal of the same tree.
// Definition of a binary tree node
// class TreeNode {
// constructor(data) {
// this.data = data;
// this.left = null;
// this.right = null;
// }
// }
import { TreeNode } from "./ds_v1/BinaryTree.js";
@optimistiks
optimistiks / sorted-array-to-bst.js
Created December 10, 2023 17:25
Given an array of integers, nums, sorted in ascending order, your task is to construct a height-balanced binary search tree (BST) from this array.
// Definition of a binary tree node
// class TreeNode {
// constructor(data) {
// this.data = data;
// this.left = null;
// this.right = null;
// }
// }
import { TreeNode } from "./ds_v1/BinaryTree.js";
@optimistiks
optimistiks / binary-tree-max-path.js
Created December 10, 2023 00:33
Given the root of a binary tree, return the maximum sum of any non-empty path.
// Definition of a binary tree node
// class TreeNode {
// constructor(data) {
// this.data = data;
// this.left = null;
// this.right = null;
// }
// }
import { TreeNode } from "./ds_v1/BinaryTree.js";