Skip to content
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
37 changes: 37 additions & 0 deletions binary-tree-level-order-traversal/evan.js
Copy link
Contributor

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로 해결했는데, 이 방법이 더 깔끔해보이네요.

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).
*/
29 changes: 29 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/evan.js
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.
*/
33 changes: 33 additions & 0 deletions remove-nth-node-from-end-of-list/evan.js
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)
*/
77 changes: 77 additions & 0 deletions reorder-list/evan.js
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Slow and Fast Algorithm으로 중앙에 위치한 노드를 찾으시고,
  2. 뒤쪽 노드들을 reverse 한 뒤
  3. 앞쪽 노드들과 뒤쪽 노드들을 번갈아가며 병합하신 것 같네요.

좋은 풀이네요! 고생하셨습니다.

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;
};
28 changes: 28 additions & 0 deletions validate-binary-search-tree/evan.js
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)
);
}