-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bd9fcbf
Added reorderList solution
nhistory cc30fce
Modified reorder list solution
nhistory 12ed6eb
Added removeNthFromEnd solution
nhistory 05600ca
Added lowestCommonAncestor solution
nhistory 252ec00
Added levelOrder solution
nhistory 97c7c53
Added validateBinarySearchTree solution
nhistory File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
11
lowest-common-ancestor-of-a-binary-search-tree/nhistory.js
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
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) |
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
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) |
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
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) |
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
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) | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
노드가 아니라 값을 변경해서 해결하신걸까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
네 맞습니다. 근데 문제를 보니 값을 바꾸지 않고 풀라는 언급이 있었네요. ㅎㅎ
그래서 노드를 수정하는 솔루션을 추가하였습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵! 저도 reverse linked list 문제에서 노드 대신 값을 바꾸는 솔루션으로 문제를 풀었는데 달레님께서 그건 문제에서 list를 뒤집으라는 본질과 벗어난다는 피드백을 받았었습니다!