Skip to content

[byol-han] WEEK 11 solutions #1567

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 5 commits into from
Jun 14, 2025
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-maximum-path-sum/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* https://leetcode.com/problems/binary-tree-maximum-path-sum/
* 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 {number}
*/
var maxPathSum = function (root) {
let maxSum = -Infinity; // global max

function dfs(node) {
if (!node) return 0;

// 왼쪽과 오른쪽 서브트리에서 최대 경로 합을 구한다
// 음수면 0으로 치환 (해당 서브트리를 포함하지 않는게 더 이득인 경우)
let leftMax = Math.max(0, dfs(node.left));
let rightMax = Math.max(0, dfs(node.right));

// 현재 노드를 루트로 하는 경로에서 최대값을 계산 (left + node + right)
let currentMax = leftMax + node.val + rightMax;

// global 최대값 갱신
maxSum = Math.max(maxSum, currentMax);

// 부모 노드로 return 시: 한쪽 방향으로만 선택 가능
return node.val + Math.max(leftMax, rightMax);
}

dfs(root);
return maxSum;
};
27 changes: 27 additions & 0 deletions missing-number/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* https://leetcode.com/problems/missing-number/
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function (nums) {
nums.sort((a, b) => a - b);

for (let i = 0; i < nums.length; i++) {
if (nums[i] !== i) {
return i; // 빠진 숫자를 찾으면 리턴
}
}

return nums.length; // 모든 숫자가 다 있으면 빠진 건 n
};

// 수학적 합 공식 이용하기 (가장 빠름)
//시간복잡도: O(n)
// 공간복잡도: O(1) (아주 효율적)

var missingNumber = function (nums) {
const n = nums.length;
const expectedSum = (n * (n + 1)) / 2;
const actualSum = nums.reduce((a, b) => a + b, 0);
return expectedSum - actualSum;
};
49 changes: 49 additions & 0 deletions reorder-list/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
*https://leetcode.com/problems/reorder-list/
* 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) {
if (!head || !head.next) return;

// 1. 중간 지점 찾기 (slow, fast 포인터 사용)
let slow = head;
let fast = head;
while (fast.next && fast.next.next) {
slow = slow.next;
fast = fast.next.next;
}

// 2. 중간 이후 리스트 뒤집기
let prev = null;
let curr = slow.next;
while (curr) {
let nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
// 중간 지점 이후는 끊기
slow.next = null;

// 3. 앞쪽 리스트와 뒤쪽 리스트 교차 병합
let first = head;
let second = prev;
while (second) {
let tmp1 = first.next;
let tmp2 = second.next;

first.next = second;
second.next = tmp1;

first = tmp1;
second = tmp2;
}
};
27 changes: 27 additions & 0 deletions same-tree/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* https://leetcode.com/problems/same-tree/description/
* 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} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
// 둘 다 null이면 같은 트리
if (p === null && q === null) return true;

// 하나는 null이고 하나는 값이 있다면 다른 트리
if (p === null || q === null) return false;

// 값이 다르면 다른 트리
if (p.val !== q.val) return false;

// 왼쪽과 오른쪽 서브트리도 각각 재귀적으로 비교
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
};