Skip to content

[Sehwan] Week7 solution with JavaScript #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 16, 2024
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
32 changes: 32 additions & 0 deletions binary-tree-level-order-traversal/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var levelOrder = function (root) {
// Edge case: If root is null, return []
if (root === null) return [];

// Create result and queue
let result = [];
let queue = [root];

// Iterate while queue.length is exist
while (queue.length) {
// Create levelArr and levelSize
let levelArr = [];
let levelSize = queue.length;
// Initiate currentNode from queue by using shift method
while (levelSize) {
const currentNode = queue.shift();
levelArr.push(currentNode.val);
// If currentNode.left is not null, push into the queue
if (currentNode.left) queue.push(currentNode.left);
// If currentNode.right is not null, push into the queue
if (currentNode.right) queue.push(currentNode.right);

levelSize--;
}
// Push levelArr into result
result.push(levelArr);
}
return result;
};

// TC: O(n)
// SC: O(n)
11 changes: 11 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var lowestCommonAncestor = function (root, p, q) {
// Iterate if statement with comparing values
while (root) {
if (root.val < p.val && root.val < q.val) root = root.right;
else if (root.val > p.val && root.val > q.val) root = root.left;
else return root;
}
};

// TC: O(n)
// SC: O(1)
33 changes: 33 additions & 0 deletions remove-nth-node-from-end-of-list/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var removeNthFromEnd = function (head, n) {
// Edge case: If the list is empty
if (!head) return null;

// Create a dummy node that points to the head
let dummy = new ListNode(0);
dummy.next = head;
let length = 0,
curr = head;

// Calculate the length of the list
while (curr) {
length++;
curr = curr.next;
}

// Find the length-n node from the beginning
length = length - n;
curr = dummy;
while (length > 0) {
length--;
curr = curr.next;
}

// Skip the desired node
curr.next = curr.next.next;

// Return the head, which may be a new head if we removed the first node
return dummy.next;
};

// TC: O(n)
// SC: O(1)
68 changes: 68 additions & 0 deletions reorder-list/nhistory.js
Copy link
Contributor

Choose a reason for hiding this comment

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

노드가 아니라 값을 변경해서 해결하신걸까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

네 맞습니다. 근데 문제를 보니 값을 바꾸지 않고 풀라는 언급이 있었네요. ㅎㅎ
그래서 노드를 수정하는 솔루션을 추가하였습니다.

Copy link
Contributor

Choose a reason for hiding this comment

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

넵! 저도 reverse linked list 문제에서 노드 대신 값을 바꾸는 솔루션으로 문제를 풀었는데 달레님께서 그건 문제에서 list를 뒤집으라는 본질과 벗어난다는 피드백을 받았었습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// First option : change node value
var reorderList = function (head) {
if (!head) return;
// Create new arr to return result and current head
let arr = [];
let curr = head;
// Make list array that has every values from linked list
let list = [];
while (curr) {
list.push(curr.val);
curr = curr.next;
}
// Iterate list array if i%2 === 0 then curr.val = list[Math.floor(i / 2)]
// If i%2 !== 0 them curr.val = list[list.length - Math.floor((i + 1) / 2)]
curr = head;
for (let i = 0; i < list.length; i++) {
if (i % 2 === 0) {
curr.val = list[Math.floor(i / 2)];
} else {
curr.val = list[list.length - Math.floor((i + 1) / 2)];
}
curr = curr.next;
}
};

// TC: O(n)
// SC: O(n)

// Second option : move node without changing values
var reorderList = function (head) {
// Edge case
if (!head) return;

// Find mid node from linked list
let slow = head,
fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}

// Reverse second half list
let prev = null,
curr = slow,
temp;
while (curr) {
temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}

// Modify linked list as followed instruction
let first = head,
second = prev;
while (second.next) {
temp = first.next;
first.next = second;
first = temp;

temp = second.next;
second.next = first;
second = temp;
}
};

// TC: O(n)
// SC: O(1)
13 changes: 13 additions & 0 deletions validate-binary-search-tree/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var isValidBST = function (root) {
return validate(root, -Infinity, Infinity);
};

function validate(node, min, max) {
if (!node) return true; // An empty tree is a valid BST
if (node.val <= min || node.val >= max) return false; // Current node's value must be between min and max

// Recursively validate the left and right subtree
return (
validate(node.left, min, node.val) && validate(node.right, node.val, max)
);
}