Skip to content

Commit 81854bf

Browse files
authored
Merge pull request #1656 from byol-han/main
[byol-han] WEEK 15 solutions
2 parents b2521d0 + 9ebf035 commit 81854bf

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* https://leetcode.com/problems/binary-tree-level-order-traversal/description/
3+
* Definition for a binary tree node.
4+
* function TreeNode(val, left, right) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.left = (left===undefined ? null : left)
7+
* this.right = (right===undefined ? null : right)
8+
* }
9+
*/
10+
/**
11+
* @param {TreeNode} root
12+
* @return {number[][]}
13+
*/
14+
var levelOrder = function (root) {
15+
const result = [];
16+
if (!root) return result;
17+
18+
const queue = [root];
19+
20+
while (queue.length > 0) {
21+
const levelSize = queue.length;
22+
const level = [];
23+
24+
for (let i = 0; i < levelSize; i++) {
25+
const node = queue.shift();
26+
level.push(node.val);
27+
28+
if (node.left) queue.push(node.left);
29+
if (node.right) queue.push(node.right);
30+
}
31+
32+
result.push(level);
33+
}
34+
35+
return result;
36+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
3+
* Definition for a binary tree node.
4+
* function TreeNode(val, left, right) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.left = (left===undefined ? null : left)
7+
* this.right = (right===undefined ? null : right)
8+
* }
9+
*/
10+
/**
11+
* @param {number[]} preorder
12+
* @param {number[]} inorder
13+
* @return {TreeNode}
14+
*/
15+
var buildTree = function (preorder, inorder) {
16+
// 해시맵을 만들어 중위 순회의 값 -> 인덱스를 빠르게 찾을 수 있도록 함
17+
const inorderMap = new Map();
18+
inorder.forEach((val, idx) => {
19+
inorderMap.set(val, idx);
20+
});
21+
22+
// preorder를 순회할 인덱스
23+
let preorderIndex = 0;
24+
25+
/**
26+
* 재귀 함수: 현재 서브트리의 중위 순회 구간(start ~ end)을 기반으로 트리를 만든다.
27+
*/
28+
function arrayToTree(left, right) {
29+
// 종료 조건: 구간이 잘못되면 노드가 없음
30+
if (left > right) return null;
31+
32+
// preorder에서 현재 루트 값 선택
33+
const rootVal = preorder[preorderIndex];
34+
preorderIndex++; // 다음 호출을 위해 인덱스 증가
35+
36+
// 현재 노드 생성
37+
const root = new TreeNode(rootVal);
38+
39+
// 루트 값의 중위 순회 인덱스 찾기
40+
const index = inorderMap.get(rootVal);
41+
42+
// 왼쪽 서브트리와 오른쪽 서브트리를 재귀적으로 생성
43+
root.left = arrayToTree(left, index - 1);
44+
root.right = arrayToTree(index + 1, right);
45+
46+
return root;
47+
}
48+
49+
// 전체 inorder 범위로 시작
50+
return arrayToTree(0, inorder.length - 1);
51+
};

house-robber-ii/byol-han.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* https://leetcode.com/problems/house-robber-ii/submissions/1686583649/
3+
* @param {number[]} nums
4+
* @return {number}
5+
*/
6+
var rob = function (nums) {
7+
if (nums.length === 1) return nums[0];
8+
9+
// Helper to solve the linear house robber problem
10+
function robLinear(houses) {
11+
let prev1 = 0; // dp[i-1]
12+
let prev2 = 0; // dp[i-2]
13+
14+
for (let money of houses) {
15+
let temp = prev1;
16+
prev1 = Math.max(prev1, prev2 + money);
17+
prev2 = temp;
18+
}
19+
20+
return prev1;
21+
}
22+
23+
// Case 1: Exclude last house
24+
let money1 = robLinear(nums.slice(0, nums.length - 1));
25+
// Case 2: Exclude first house
26+
let money2 = robLinear(nums.slice(1));
27+
28+
return Math.max(money1, money2);
29+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* https://leetcode.com/problems/longest-palindromic-substring/submissions/1694755721/
3+
* @param {string} s
4+
* @return {string}
5+
*/
6+
var longestPalindrome = function (s) {
7+
// 결과로 반환할 가장 긴 팰린드롬 초기값
8+
let result = '';
9+
10+
// 팰린드롬의 중심에서 양쪽으로 확장하는 함수
11+
function expandAroundCenter(left, right) {
12+
// 왼쪽이 0 이상, 오른쪽이 문자열 길이 이하이며
13+
// 양쪽 문자가 같으면 계속 확장
14+
while (left >= 0 && right < s.length && s[left] === s[right]) {
15+
left--;
16+
right++;
17+
}
18+
// while문을 빠져나오면 팰린드롬이 아님.
19+
// 현재 찾은 팰린드롬 반환
20+
return s.slice(left + 1, right);
21+
}
22+
23+
// 문자열의 각 문자를 중심으로 확장 시도
24+
for (let i = 0; i < s.length; i++) {
25+
// 홀수 길이 팰린드롬 (중심이 한 글자)
26+
const oddPalindrome = expandAroundCenter(i, i);
27+
// 짝수 길이 팰린드롬 (중심이 두 글자)
28+
const evenPalindrome = expandAroundCenter(i, i + 1);
29+
30+
// 더 긴 팰린드롬 선택
31+
if (oddPalindrome.length > result.length) {
32+
result = oddPalindrome;
33+
}
34+
if (evenPalindrome.length > result.length) {
35+
result = evenPalindrome;
36+
}
37+
}
38+
39+
// 결과 반환
40+
return result;
41+
};

subtree-of-another-tree/byol-han.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* https://leetcode.com/problems/subtree-of-another-tree/description/
3+
* Definition for a binary tree node.
4+
* function TreeNode(val, left, right) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.left = (left===undefined ? null : left)
7+
* this.right = (right===undefined ? null : right)
8+
* }
9+
*/
10+
/**
11+
* @param {TreeNode} root
12+
* @param {TreeNode} subRoot
13+
* @return {boolean}
14+
*/
15+
var isSubtree = function (root, subRoot) {
16+
// If the main tree is empty, it can't contain subRoot
17+
if (!root) return false;
18+
19+
// If the trees rooted at current node are the same, return true
20+
if (isSameTree(root, subRoot)) {
21+
return true;
22+
}
23+
24+
// Otherwise, recursively check left and right subtrees
25+
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
26+
};
27+
28+
function isSameTree(s, t) {
29+
// If both nodes are null, trees are identical at this branch
30+
if (!s && !t) return true;
31+
32+
// If only one is null, trees are not identical
33+
if (!s || !t) return false;
34+
35+
// If current node values are different, trees are not identical
36+
if (s.val !== t.val) return false;
37+
38+
// Recursively check left and right children
39+
return isSameTree(s.left, t.left) && isSameTree(s.right, t.right);
40+
}

0 commit comments

Comments
 (0)