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
70 changes: 70 additions & 0 deletions maximum-depth-of-binary-tree/leehyeyun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* 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);
}
/*
이진 트리(Binary Tree)의 root 노드가 주어졌을 때,
트리의 최대 깊이(maximum depth)를 반환하는 함수.

최대 깊이란:
- 루트(root)에서 가장 먼 리프(leaf) 노드까지
도달하는 경로에 포함된 "노드의 개수"를 의미함.

Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 3
설명:
트리 구조는 다음과 같으며,
가장 깊은 경로(3 → 20 → 15 또는 3 → 20 → 7)의 노드 수는 3.

Example 2:
Input: root = [1,null,2]
Output: 2
설명:
트리 구조는 1 → 2 형태이며 노드 수 2가 최대 깊이.

Constraints:
- 트리의 노드 개수: 0 ~ 10^4
- 노드 값 범위: -100 ~ 100
*/

/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {

if (root === null) return 0;

const left = maxDepth(root.left);
const right = maxDepth(root.right);

return 1 + Math.max(left, right);
};

// example1
const example1 = new TreeNode(
3,
new TreeNode(9),
new TreeNode(20,
new TreeNode(15),
new TreeNode(7)
)
);

// example2
const example2 = new TreeNode(
1,
null,
new TreeNode(2)
);


// maxDepth 함수 결과 출력
console.log("Example 1 maxDepth:", maxDepth(example1));
console.log("Example 2 maxDepth:", maxDepth(example2));

91 changes: 91 additions & 0 deletions merge-two-sorted-lists/leehyeyun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/

/*
두 개의 정렬된 연결 리스트 list1과 list2의 head가 주어진다.

두 리스트의 노드를 이어 붙여 하나의 정렬된 리스트를 만들고,
그 병합된 연결 리스트의 head를 반환하는 함수.

Example 1:
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]

Example 2:
Input: list1 = []
list2 = []
Output: []

Example 3:
Input: list1 = []
list2 = [0]
Output: [0]

Constraints:
- 두 리스트의 노드 개수: 0 ~ 50
- 노드 값 범위: -100 ~ 100
- list1과 list2는 모두 오름차순(Non-decreasing order)으로 정렬됨
*/
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/

function ListNode(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}

var mergeTwoLists = function(list1, list2) {
//새로운 노드 생성 -> -1이라는 값은 쓰레기값
let dummy = new ListNode(-1);
let current = dummy;

while (list1 !== null && list2 !== null) {
//더 작은 값을 가진 노드를 현재 노드 뒤에 붙여줌
if (list1.val < list2.val) {
current.next = list1;
list1 = list1.next; //다음 노드로 이동
} else {
current.next = list2;
list2 = list2.next; //다음 노드로 이동
}
current = current.next; //새 노드를 붙인 뒤 이동
}

// 둘 중 하나가 남아 있으면 이어붙이기
if (list1 !== null) current.next = list1;
if (list2 !== null) current.next = list2;
Comment on lines +63 to +64
Copy link
Member

Choose a reason for hiding this comment

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

저는 처음에 왜 붙어야 하는지 몰랐으나 조금 생각해보면 while문이 끝나는 조건은 둘중 하나가 Null이 될때이므로 나머지는 남아 있다는 의미이므로 남은 부분에 대해 붙여줘야하는 로직이 들어가는 것을 알게 되었습니다.


//dummy -1값은 제외하고 return하기 위해 dummy.next를 반환
return dummy.next;
};

// Example 1
const list1 = new ListNode(1, new ListNode(2, new ListNode(4)));
const list2 = new ListNode(1, new ListNode(3, new ListNode(4)));

console.log("Example 1:");
console.log(JSON.stringify(mergeTwoLists(list1, list2), null, 2));

// Example 2
const list1_ex2 = null;
const list2_ex2 = null;

console.log("Example 2:");
console.log(JSON.stringify(mergeTwoLists(list1_ex2, list2_ex2), null, 2));

// Example 3
const list1_ex3 = null;
const list2_ex3 = new ListNode(0);

console.log("Example 3:");
console.log(JSON.stringify(mergeTwoLists(list1_ex3, list2_ex3), null, 2));