diff --git a/maximum-depth-of-binary-tree/seongmin36.js b/maximum-depth-of-binary-tree/seongmin36.js new file mode 100644 index 0000000000..e7113e9a9a --- /dev/null +++ b/maximum-depth-of-binary-tree/seongmin36.js @@ -0,0 +1,27 @@ +/** +이 문제를 푸는 핵심은 DFS(깊이 우선 탐색)다. +왼쪽으로, 오른쪽으로 쭉 들어가는 값을 반환해서 변수에 저장한다. +이를 재귀호출하면 쉽게 해결할 수 있다. + */ +/** + * 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} + */ +function maxDepth(root) { + if (root === null) return 0; + + let left_depth = maxDepth(root.left); + let right_depth = maxDepth(root.right); + + let count = Math.max(left_depth, right_depth) + 1; + + return count; +} diff --git a/merge-two-sorted-lists/seongmin36.js b/merge-two-sorted-lists/seongmin36.js new file mode 100644 index 0000000000..23376925a3 --- /dev/null +++ b/merge-two-sorted-lists/seongmin36.js @@ -0,0 +1,45 @@ +/** +ListNode는 파라미터로 current value와 next value를 지닌다. +배열 메서드도 사용할 수 없다. + +나는 원초적인 방법으로 해결해보았다. +result 배열에 모든 수를 다 넣고, sort()하는 것이다. +여기서 중요한 점은 '어떻게 배열 → ListNode로 변경하느냐'이다. +[1, 2, 3] 배열을 1 → 2 → 3 리스트 노드를 만들기 위해서는 컴퓨터 구조상 역방향인 3부터 가져와야한다. +이때 사용한 메서드는 'reduceRight()'다. +reduceRight() 메서드는 reduce() 작업순서의 역방향이다. + +TC : O(nLogN) → sort() +SC : O(n) + */ +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} list1 + * @param {ListNode} list2 + * @return {ListNode} + */ +function mergeTwoLists(list1, list2) { + let result = []; + let node1 = list1; + let node2 = list2; + + while (node1 !== null) { + result.push(node1.val); + node1 = node1.next; + } + + while (node2 !== null) { + result.push(node2.val); + node2 = node2.next; + } + + return result + .sort((a, b) => a - b) + .reduceRight((next, val) => new ListNode(val, next), null); +}