-
-
Notifications
You must be signed in to change notification settings - Fork 245
Evan: Week 7 #131
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
Evan: Week 7 #131
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
14ef09a
solve: binary tree level order traversal
sounmind 05d1725
solve: reorder list
sounmind 5e53d5f
solve: remove nth node from end of list
sounmind cfe38a9
solve: lowest common ancestor of a BST
sounmind 2c56ca9
solve: validate binary search tree
sounmind 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,37 @@ | ||
/** | ||
* @param {TreeNode} root | ||
* @return {number[][]} | ||
*/ | ||
var levelOrder = function (root) { | ||
const result = []; | ||
|
||
const dfs = (node, level) => { | ||
if (!node) { | ||
return; | ||
} | ||
|
||
if (!result[level]) { | ||
result[level] = []; | ||
} | ||
|
||
result[level].push(node.val); | ||
|
||
dfs(node.left, level + 1); | ||
dfs(node.right, level + 1); | ||
}; | ||
|
||
dfs(root, 0); | ||
|
||
return result; | ||
}; | ||
|
||
/** | ||
* Time Complexity: O(n) | ||
* - The function visits each node exactly once, making it O(n). | ||
* - Here, n is the number of nodes in the binary tree. | ||
* | ||
* Space Complexity: O(n) | ||
* - The space complexity is determined by the recursion stack. | ||
* - In the worst case, the recursion stack could store all nodes in the binary tree. | ||
* - Thus, the space complexity is O(n). | ||
*/ |
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,29 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @param {TreeNode} p | ||
* @param {TreeNode} q | ||
* @return {TreeNode} | ||
*/ | ||
var lowestCommonAncestor = function (root, p, q) { | ||
if (root.val < p.val && root.val < q.val) { | ||
return lowestCommonAncestor(root.right, p, q); | ||
} | ||
|
||
if (root.val > p.val && root.val > q.val) { | ||
return lowestCommonAncestor(root.left, p, q); | ||
} | ||
|
||
return root; | ||
}; | ||
|
||
/** | ||
* Time Complexity: O(h), where h is the height of the binary search tree. | ||
* Space Complexity: O(h), where h is the height of the binary search tree. | ||
*/ |
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 @@ | ||
/** | ||
* @param {ListNode} head | ||
* @param {number} n | ||
* @return {ListNode} | ||
*/ | ||
function removeNthFromEnd(head, n) { | ||
let dummy = new ListNode(0); | ||
dummy.next = head; | ||
let first = dummy; | ||
let second = dummy; | ||
|
||
// Move the first pointer n+1 steps ahead | ||
for (let i = 0; i <= n; i++) { | ||
first = first.next; | ||
} | ||
|
||
// Move both pointers until the first pointer reaches the end | ||
// Second pointer will be at the (n+1)th node from the end | ||
while (first !== null) { | ||
first = first.next; | ||
second = second.next; | ||
} | ||
|
||
// Remove the nth node from the end | ||
second.next = second.next.next; | ||
|
||
return dummy.next; | ||
} | ||
|
||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ |
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.
좋은 풀이네요! 고생하셨습니다. |
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,77 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @return {void} Do not return anything, modify head in-place instead. | ||
*/ | ||
var reorderList = function (head) { | ||
const middleNode = findMiddleNode(head); | ||
let reversedHalf = reverseList(middleNode.next); | ||
middleNode.next = null; | ||
|
||
let half = head; | ||
|
||
while (reversedHalf) { | ||
const nextForward = half.next; | ||
const nextBackward = reversedHalf.next; | ||
|
||
half.next = reversedHalf; | ||
reversedHalf.next = nextForward; | ||
|
||
reversedHalf = nextBackward; | ||
half = nextForward; | ||
} | ||
}; | ||
/** | ||
* Time Complexity: O(n) | ||
* - Finding the middle node takes O(n). | ||
* - Reversing the second half takes O(n). | ||
* - Merging the two halves takes O(n). | ||
* - Overall, the time complexity is O(n) + O(n) + O(n) = O(n). | ||
* | ||
* Space Complexity: O(1) | ||
* - We only use a constant amount of extra space (pointers), | ||
* so the space complexity is O(1). | ||
*/ | ||
|
||
/** | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var reverseList = function (head) { | ||
let currentNode = head; | ||
let previousNode = null; | ||
|
||
while (currentNode !== null) { | ||
const nextNode = currentNode.next; | ||
|
||
currentNode.next = previousNode; | ||
previousNode = currentNode; | ||
currentNode = nextNode; | ||
} | ||
|
||
return previousNode; | ||
}; | ||
|
||
/** | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var findMiddleNode = function (head) { | ||
if (!head) return null; | ||
|
||
let slow = head; | ||
let fast = head; | ||
|
||
while (fast !== null && fast.next !== null) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
} | ||
|
||
return slow; | ||
}; |
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,28 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val, left, right) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @return {boolean} | ||
*/ | ||
function isValidBST(root, low = -Infinity, high = Infinity) { | ||
if (root === null) { | ||
return true; | ||
} | ||
|
||
if (root.val <= low || root.val >= high) { | ||
return false; | ||
} | ||
|
||
return ( | ||
// left root should be less than current root | ||
isValidBST(root.left, low, root.val) && | ||
// right root should be greater than current root | ||
isValidBST(root.right, root.val, high) | ||
); | ||
} |
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.
Depth First Search 방법으로 문제를 해결하셨군요! 저는 Breadth First Search로 해결했는데, 이 방법이 더 깔끔해보이네요.