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
| function checkLevelOrderBST(levelOrder) { | |
| if (levelOrder.length === 0) return true; | |
| let i = 0; | |
| const n = levelOrder.length; | |
| // Stack to store nodes with their min and max constraints | |
| const stack = []; | |
| stack.push({ | |
| data: levelOrder[0], | |
| min: -Infinity, | |
| max: Infinity, |
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(data) { | |
| this.data = data; | |
| this.left = null; | |
| this.right = null; | |
| } | |
| } | |
| function insert(root, data) { | |
| if (root === null) { | |
| return new Node(data); |
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
| function bfs(n, m, edges, s) { | |
| // Create adjacency list for the graph | |
| const adjList = new Array(n + 1).fill().map(() => []); | |
| // Build the graph from edges | |
| for (const [u, v] of edges) { | |
| adjList[u].push(v); | |
| adjList[v].push(u); // Undirected graph | |
| } | |
| // Initialize distances array with -1 (unreachable) | |
| const distances = new Array(n + 1).fill(-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
| function diagonalDifference(arr) { | |
| let left = 0; | |
| let right = 0; | |
| const n = arr.length; | |
| for (let i = 0; i < n; i++) { | |
| left += arr[i][i]; | |
| right += arr[i][n - i - 1]; | |
| } | |
| return Math.abs(left - right); | |
| } |
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
| // Breadth First Search (BFS) | |
| function levelOrder(root) { | |
| if (!root) return; | |
| const queue = [root]; | |
| const result = []; | |
| let index = 0; | |
| while (index < queue.length) { | |
| const currentNode = queue[index++]; | |
| result.push(currentNode.data); | |
| if (currentNode.left) queue.push(currentNode.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
| function isBalanced(s) { | |
| const pattern = /\[\]|\(\)|\{\}/g; | |
| while (pattern.test(s)) { | |
| s = s.replace(pattern, ""); | |
| } | |
| return s ? "NO" : "YES"; | |
| } |
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
| function analyzeFrequencies(str) { | |
| const frequencies = {}; | |
| for (const char of str) { | |
| frequencies[char] = (frequencies[char] || 0) + 1; | |
| } | |
| return frequencies; | |
| } | |
| // Factory function for leaf node | |
| function createLeaf(char, frequency) { | |
| return { |
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(data) { | |
| this.data = data; | |
| this.left = null; | |
| this.right = null; | |
| } | |
| } | |
| function insert(root, data) { | |
| if (root === null) { | |
| return new Node(data); |
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(data) { | |
| this.data = data; | |
| this.left = null; | |
| this.right = null; | |
| } | |
| } | |
| function insert(root, data) { | |
| if (root === null) { | |
| return new Node(data); |
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
| function height(root) { | |
| if (root === null) return -1; | |
| return 1 + Math.max(height(root.left), height(root.right)); | |
| } |