Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions binary-tree-level-order-traversal/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time Complexity: O(n)
// Space Complexity: O(n)

var levelOrder = function (root) {
// if the root is null, return an empty array.
if (root === null) return [];

const result = [];
const queue = [root];

// while there are nodes in the queue,
while (queue.length > 0) {
const levelSize = queue.length;
const currentLevel = [];

// loop nodes in the current level.
for (let i = 0; i < levelSize; i++) {
// dequeue the front node.
const currentNode = queue.shift();
// add value to the current level array.
currentLevel.push(currentNode.val);
// enqueue left child if exists.
if (currentNode.left) queue.push(currentNode.left);
// enqueue right child if exists.
if (currentNode.right) queue.push(currentNode.right);
}

// add the current level array to the result.
result.push(currentLevel);
}

return result;
};
26 changes: 26 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time Complexity: O(n)
// Space Complexity: O(1)

var lowestCommonAncestor = function (root, p, q) {
// start from the root.
let current = root;

// traverse the tree.
while (current !== null) {
// if both p and q are greater than current node, LCA lies in the right.
if (p.val > current.val && q.val > current.val) {
current = current.right;
}
// if both p and q are smaller than current node, LCA lies in the left.
else if (p.val < current.val && q.val < current.val) {
current = current.left;
}
// if one of p or q is on one side and the other is on the other side, It's LCA.
else {
return current;
}
}

// if the tree is empty.
return null;
};
32 changes: 32 additions & 0 deletions remove-nth-node-from-end-of-list/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Time Complexity: O(n)
// Space Complexity: O(1)

var removeNthFromEnd = function (head, n) {
// calculate the length of the linked list.
let length = 0;
let current = head;
while (current !== null) {
length++;
current = current.next;
}

// determine the position to remove from the start.
let removeIndex = length - n;

// if the node to be removed is the head, return the next node.
if (removeIndex === 0) {
return head.next;
}

// traverse to the node just before the node to be removed.
current = head;
for (let i = 0; i < removeIndex - 1; i++) {
current = current.next;
}

// remove the nth node from the end.
current.next = current.next.next;

// return the modified list.
return head;
};
27 changes: 27 additions & 0 deletions reorder-list/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time Complexity: O(n)
// Space Complexity: O(n)

var reorderList = function (head) {
// push all nodes onto a stack.
let stack = [];
let current = head;
while (current) {
stack.push(current);
current = current.next;
}

// reorder the list.
let n = stack.length;
current = head;

for (let i = 0; i < Math.floor(n / 2); i++) {
let next = current.next;
let last = stack.pop();

current.next = last;
last.next = next;
current = next;
}
// ensure the last node points to null.
if (current) current.next = null;
};
34 changes: 34 additions & 0 deletions validate-binary-search-tree/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Time Complexity: O(n)
// Space Complexity: O(n)

var isValidBST = function (root) {
if (root === null) {
return true;
}

// initialize a queue for BFS.
let queue = [];
queue.push({ node: root, min: -Infinity, max: Infinity });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS는 Infinity 라는 게 있군요. 신기하네요!


while (queue.length > 0) {
// dequeue the front one.
let { node, min, max } = queue.shift();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

간결하고 좋네요! 👍


// check the BST for the current node.
if (node.val <= min || node.val >= max) {
return false;
}

// enqueue the left child with updated min and max.
if (node.left !== null) {
queue.push({ node: node.left, min: min, max: node.val });
}

// enqueue the right child with updated min and max.
if (node.right !== null) {
queue.push({ node: node.right, min: node.val, max: max });
}
}

return true;
};