-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Helena] Week 7 solutions #133
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
1977bab
Add week 7 solutions : validateBinarySearchTree
yolophg 6ee5bee
Add week 7 solutions : binaryTreeLevelOrderTraversal
yolophg e25d7e3
Merge branch 'DaleStudy:main' into main
yolophg c0da1e6
Add week 7 solutions : reorderList
yolophg f42bd6e
Add week 7 solutions : removeNthNodeFromEndOfList
yolophg bff7a01
Add week 7 solutions : lowestCommonAncestorOfABinarySearchTree
yolophg 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,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; | ||
}; |
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,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; | ||
}; |
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 @@ | ||
// 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; | ||
}; |
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,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; | ||
}; |
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,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 }); | ||
|
||
while (queue.length > 0) { | ||
// dequeue the front one. | ||
let { node, min, max } = queue.shift(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}; |
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.
JS는
Infinity
라는 게 있군요. 신기하네요!