Skip to content

Commit 1061d0d

Browse files
authored
Merge pull request #129 from nhistory/week7
[Sehwan] Week7 solution with JavaScript
2 parents d0eae9b + 97c7c53 commit 1061d0d

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var levelOrder = function (root) {
2+
// Edge case: If root is null, return []
3+
if (root === null) return [];
4+
5+
// Create result and queue
6+
let result = [];
7+
let queue = [root];
8+
9+
// Iterate while queue.length is exist
10+
while (queue.length) {
11+
// Create levelArr and levelSize
12+
let levelArr = [];
13+
let levelSize = queue.length;
14+
// Initiate currentNode from queue by using shift method
15+
while (levelSize) {
16+
const currentNode = queue.shift();
17+
levelArr.push(currentNode.val);
18+
// If currentNode.left is not null, push into the queue
19+
if (currentNode.left) queue.push(currentNode.left);
20+
// If currentNode.right is not null, push into the queue
21+
if (currentNode.right) queue.push(currentNode.right);
22+
23+
levelSize--;
24+
}
25+
// Push levelArr into result
26+
result.push(levelArr);
27+
}
28+
return result;
29+
};
30+
31+
// TC: O(n)
32+
// SC: O(n)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var lowestCommonAncestor = function (root, p, q) {
2+
// Iterate if statement with comparing values
3+
while (root) {
4+
if (root.val < p.val && root.val < q.val) root = root.right;
5+
else if (root.val > p.val && root.val > q.val) root = root.left;
6+
else return root;
7+
}
8+
};
9+
10+
// TC: O(n)
11+
// SC: O(1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var removeNthFromEnd = function (head, n) {
2+
// Edge case: If the list is empty
3+
if (!head) return null;
4+
5+
// Create a dummy node that points to the head
6+
let dummy = new ListNode(0);
7+
dummy.next = head;
8+
let length = 0,
9+
curr = head;
10+
11+
// Calculate the length of the list
12+
while (curr) {
13+
length++;
14+
curr = curr.next;
15+
}
16+
17+
// Find the length-n node from the beginning
18+
length = length - n;
19+
curr = dummy;
20+
while (length > 0) {
21+
length--;
22+
curr = curr.next;
23+
}
24+
25+
// Skip the desired node
26+
curr.next = curr.next.next;
27+
28+
// Return the head, which may be a new head if we removed the first node
29+
return dummy.next;
30+
};
31+
32+
// TC: O(n)
33+
// SC: O(1)

reorder-list/nhistory.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// First option : change node value
2+
var reorderList = function (head) {
3+
if (!head) return;
4+
// Create new arr to return result and current head
5+
let arr = [];
6+
let curr = head;
7+
// Make list array that has every values from linked list
8+
let list = [];
9+
while (curr) {
10+
list.push(curr.val);
11+
curr = curr.next;
12+
}
13+
// Iterate list array if i%2 === 0 then curr.val = list[Math.floor(i / 2)]
14+
// If i%2 !== 0 them curr.val = list[list.length - Math.floor((i + 1) / 2)]
15+
curr = head;
16+
for (let i = 0; i < list.length; i++) {
17+
if (i % 2 === 0) {
18+
curr.val = list[Math.floor(i / 2)];
19+
} else {
20+
curr.val = list[list.length - Math.floor((i + 1) / 2)];
21+
}
22+
curr = curr.next;
23+
}
24+
};
25+
26+
// TC: O(n)
27+
// SC: O(n)
28+
29+
// Second option : move node without changing values
30+
var reorderList = function (head) {
31+
// Edge case
32+
if (!head) return;
33+
34+
// Find mid node from linked list
35+
let slow = head,
36+
fast = head;
37+
while (fast && fast.next) {
38+
slow = slow.next;
39+
fast = fast.next.next;
40+
}
41+
42+
// Reverse second half list
43+
let prev = null,
44+
curr = slow,
45+
temp;
46+
while (curr) {
47+
temp = curr.next;
48+
curr.next = prev;
49+
prev = curr;
50+
curr = temp;
51+
}
52+
53+
// Modify linked list as followed instruction
54+
let first = head,
55+
second = prev;
56+
while (second.next) {
57+
temp = first.next;
58+
first.next = second;
59+
first = temp;
60+
61+
temp = second.next;
62+
second.next = first;
63+
second = temp;
64+
}
65+
};
66+
67+
// TC: O(n)
68+
// SC: O(1)
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var isValidBST = function (root) {
2+
return validate(root, -Infinity, Infinity);
3+
};
4+
5+
function validate(node, min, max) {
6+
if (!node) return true; // An empty tree is a valid BST
7+
if (node.val <= min || node.val >= max) return false; // Current node's value must be between min and max
8+
9+
// Recursively validate the left and right subtree
10+
return (
11+
validate(node.left, min, node.val) && validate(node.right, node.val, max)
12+
);
13+
}

0 commit comments

Comments
 (0)