Skip to content

Instantly share code, notes, and snippets.

View kshirish's full-sized avatar
👨‍💻

k kshirish

👨‍💻
View GitHub Profile
@kshirish
kshirish / inverseBinaryTree.js
Created August 14, 2024 06:45
Binary Tree - inverse (mirror)
// Example 1:
// Input: root = [4,2,7,1,3,6,9]
// Output: [4,7,2,9,6,3,1]
// Example 2:
// Input: root = [4,2,7,1,3,6]
// Output: [4,7,2,6,3,1]
// Example 3:
// Input: root = [2,1,3]
@kshirish
kshirish / binarySearchTree.js
Created August 13, 2024 04:07
Binary Search Tree - Insert, Delete
class TreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
@kshirish
kshirish / binaryTree.js
Created August 13, 2024 03:54
Binary Tree Array - Insert, Delete
function insert(tree, value) {
tree.push(value);
return tree;
}
function deleteNode(tree, value) {
const index = tree.indexOf(value);
if (index === -1) {
return tree;
@kshirish
kshirish / binaryTree.js
Created August 13, 2024 03:44
Binary Tree Node - Insert, Delete
// Root
// |
// |
// 11 12
// | |
// | |
// 13 14 15 16
// |
// |
// 17 18
@kshirish
kshirish / getChildrenByNode.js
Created August 13, 2024 03:06
Binary Tree Array - children
// Binary tree represented as an array
// 1
// / \
// 2 3
// / \ \
// 4 5 6
const tree = [1, 2, 3, 4, 5, null, 6];
@kshirish
kshirish / dfs.js
Created August 13, 2024 02:58
Binary Tree Array
// Binary tree represented as an array
// 1
// / \
// 2 3
// / \ \
// 4 5 6
const tree = [1, 2, 3, 4, 5, null, 6];
function dfs(tree) {
@kshirish
kshirish / bfs.js
Last active August 13, 2024 02:59
Binary Tree Array
// Binary tree represented as an array
// 1
// / \
// 2 3
// / \ \
// 4 5 6
const tree = [1, 2, 3, 4, 5, null, 6];
function bfs(tree) {
@kshirish
kshirish / post-order.js
Created August 13, 2024 02:45
Binary Tree - A DFS type order
// Root
// |
// |
// 11 12
// | |
// | |
// 13 14 15 16
// |
// |
// 17 18
@kshirish
kshirish / in-order.js
Created August 13, 2024 02:44
Binary Tree - A DFS type order
// Root
// |
// |
// 11 12
// | |
// | |
// 13 14 15 16
// |
// |
// 17 18
@kshirish
kshirish / pre-order.js
Created August 13, 2024 02:44
Binary Tree - A DFS type order
// Root
// |
// |
// 11 12
// | |
// | |
// 13 14 15 16
// |
// |
// 17 18