diff --git a/binary-tree-maximum-path-sum/byol-han.js b/binary-tree-maximum-path-sum/byol-han.js new file mode 100644 index 000000000..53879e1ae --- /dev/null +++ b/binary-tree-maximum-path-sum/byol-han.js @@ -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; +}; diff --git a/missing-number/byol-han.js b/missing-number/byol-han.js new file mode 100644 index 000000000..47b78bc11 --- /dev/null +++ b/missing-number/byol-han.js @@ -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; +}; diff --git a/reorder-list/byol-han.js b/reorder-list/byol-han.js new file mode 100644 index 000000000..957b674cd --- /dev/null +++ b/reorder-list/byol-han.js @@ -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; + } +}; diff --git a/same-tree/byol-han.js b/same-tree/byol-han.js new file mode 100644 index 000000000..40b3735fe --- /dev/null +++ b/same-tree/byol-han.js @@ -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); +};