Skip to content

Commit d004911

Browse files
authored
Merge branch 'main' into feat/week3
2 parents f26c3e5 + 6940cbb commit d004911

File tree

199 files changed

+5711
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

199 files changed

+5711
-3
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: ๐Ÿ”„ Integration
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
linelint:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
- uses: fernandrone/[email protected]

โ€Ž.linelint.ymlโ€Ž

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 'true' will fix files
2+
autofix: false
3+
4+
# list of paths to ignore, uses gitignore syntaxes (executes before any rule)
5+
ignore:
6+
- "*.md"
7+
8+
rules:
9+
# checks if file ends in a newline character
10+
end-of-file:
11+
# set to true to enable this rule
12+
enable: true
13+
14+
# set to true to disable autofix (if enabled globally)
15+
disable-autofix: false
16+
17+
# if true also checks if file ends in a single newline character
18+
single-new-line: false

โ€Žbinary-tree-level-order-traversal/WhiteHyun.swiftโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,4 @@ class Solution {
104104

105105
return array
106106
}
107-
}
107+
}

โ€Žcombination-sum/EGON.pyโ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def solveWithDFS(self, candidates: List[int], target: int) -> List[List[int]]:
4444
def solveWithBackTracking(self, candidates: List[int], target: int) -> List[List[int]]:
4545
return []
4646

47-
4847
class _LeetCodeTestCases(TestCase):
4948
def test_1(self):
5049
candidates = [2, 3, 6, 7]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Definition for a binary tree node.
2+
class TreeNode {
3+
val: number;
4+
left: TreeNode | null;
5+
right: TreeNode | null;
6+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
7+
this.val = val === undefined ? 0 : val;
8+
this.left = left === undefined ? null : left;
9+
this.right = right === undefined ? null : right;
10+
}
11+
}
12+
13+
// T.C: O(N)
14+
// S.C: O(N^2) - Slice makes n-1, n-2, ..., 1 for n times. So, it's O(N^2).
15+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
16+
if (preorder.length === 0 || inorder.length === 0) {
17+
return null;
18+
}
19+
const root = new TreeNode(preorder[0]);
20+
const idx = inorder.indexOf(preorder[0]);
21+
root.left = buildTree(preorder.slice(1, idx + 1), inorder.slice(0, idx));
22+
root.right = buildTree(preorder.slice(idx + 1), inorder.slice(idx + 1));
23+
24+
return root;
25+
}
26+
27+
// Not using slice. but I think it's not necessary... first solution is more readable. and that's not so bad.
28+
// T.C: O(N)
29+
// S.C: O(N)
30+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
31+
// this tree is consist of unique values
32+
const inorderMap = new Map<number, number>();
33+
for (const [i, val] of inorder.entries()) {
34+
inorderMap.set(val, i);
35+
}
36+
37+
function helper(preLeft: number, preRight: number, inLeft: number, inRight: number): TreeNode | null {
38+
if (preLeft > preRight) return null;
39+
40+
const rootValue = preorder[preLeft];
41+
const root = new TreeNode(rootValue);
42+
const inRootIdx = inorderMap.get(rootValue)!;
43+
44+
const leftSize = inRootIdx - inLeft;
45+
46+
root.left = helper(preLeft + 1, preLeft + leftSize, inLeft, inRootIdx - 1);
47+
root.right = helper(preLeft + leftSize + 1, preRight, inRootIdx + 1, inRight);
48+
49+
return root;
50+
}
51+
52+
return helper(0, preorder.length - 1, 0, inorder.length - 1);
53+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
private int i, p;
3+
public TreeNode buildTree(int[] preorder, int[] inorder) {
4+
// Time complexity: O(n)
5+
// Space complexity: O(n)
6+
return builder(preorder, inorder, Integer.MIN_VALUE);
7+
}
8+
9+
private TreeNode builder(int[] preorder, int[] inorder, int stop) {
10+
if (p >= preorder.length) return null;
11+
if (inorder[i] == stop) {
12+
i += 1;
13+
return null;
14+
}
15+
16+
TreeNode node = new TreeNode(preorder[p]);
17+
p += 1;
18+
19+
node.left = builder(preorder, inorder, node.val);
20+
node.right = builder(preorder, inorder, stop);
21+
return node;
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
# T: O(N)
9+
# S: O(N)
10+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
11+
# preorder : root - left - right
12+
# inorder : left - root - right
13+
if not preorder and not inorder:
14+
return None
15+
16+
root = TreeNode(preorder[0])
17+
mid = inorder.index(preorder[0])
18+
19+
root.left = self.buildTree(preorder[1 : mid + 1], inorder[:mid])
20+
root.right = self.buildTree(preorder[mid + 1 :], inorder[mid+1:])
21+
22+
return root
23+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* For the number of given nodes N,
3+
*
4+
* Time complexity: O(N)
5+
*
6+
* Space complexity: O(N) at worst
7+
*/
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* struct TreeNode {
12+
* int val;
13+
* TreeNode *left;
14+
* TreeNode *right;
15+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
17+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
18+
* };
19+
*/
20+
class Solution {
21+
public:
22+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
23+
unordered_map<int, int> inorder_index_map;
24+
stack<TreeNode*> tree_stack;
25+
26+
for (int i = 0; i < inorder.size(); i++) inorder_index_map[inorder[i]] = i;
27+
28+
TreeNode* root = new TreeNode(preorder[0]);
29+
tree_stack.push(root);
30+
31+
for (int i = 1; i < preorder.size(); i++) {
32+
TreeNode* curr = new TreeNode(preorder[i]);
33+
34+
if (inorder_index_map[curr->val] < inorder_index_map[tree_stack.top()->val]) {
35+
tree_stack.top()->left = curr;
36+
} else {
37+
TreeNode* parent;
38+
while (!tree_stack.empty() && inorder_index_map[curr->val] > inorder_index_map[tree_stack.top()->val]) {
39+
parent = tree_stack.top();
40+
tree_stack.pop();
41+
}
42+
parent->right = curr;
43+
}
44+
tree_stack.push(curr);
45+
}
46+
47+
return root;
48+
}
49+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
/**
3+
* * ๋ฌธ์ œ์—์„œ ์ •์˜๋œ ํƒ€์ž…
4+
*/
5+
export class TreeNode {
6+
val: number
7+
left: TreeNode | null
8+
right: TreeNode | null
9+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
10+
this.val = (val === undefined ? 0 : val)
11+
this.left = (left === undefined ? null : left)
12+
this.right = (right === undefined ? null : right)
13+
}
14+
}
15+
16+
/**
17+
* ! ๋ฌธ์ œ์—์„œ์˜ Output๊ณผ ์‹ค์ œ ์ •์˜๋œ, ์‚ฌ์šฉ๋˜๋Š” output์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ ๋ฒˆ ๋ณ€ํ™˜ ์ž‘์—…์„ ๊ฑฐ์ฒ˜์•ผํ•จ. (์‹ค์ œ ์ œ์ถœ ์‹œ ์ œ์™ธํ•œ ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค.)
18+
*/
19+
// function treeToArray(root: TreeNode | null): (number | null)[] {
20+
// if (!root) return [];
21+
// const result: (number | null)[] = [];
22+
// const queue: (TreeNode | null)[] = [root];
23+
// while (queue.length > 0) {
24+
// const node = queue.shift();
25+
// if (node) {
26+
// result.push(node.val);
27+
// queue.push(node.left);
28+
// queue.push(node.right);
29+
// } else {
30+
// result.push(null);
31+
// }
32+
// }
33+
// while (result[result.length - 1] === null) result.pop();
34+
// return result;
35+
// }
36+
37+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
38+
if (preorder.length === 0 || inorder.length === 0) return null;
39+
40+
const rootVal = preorder[0];
41+
const inorderIndex = inorder.indexOf(rootVal);
42+
const leftInorder = inorder.slice(0, inorderIndex);
43+
44+
return new TreeNode(
45+
rootVal,
46+
buildTree(
47+
preorder.slice(1, 1 + leftInorder.length),
48+
leftInorder
49+
),
50+
buildTree(
51+
preorder.slice(1 + leftInorder.length),
52+
inorder.slice(inorderIndex + 1)
53+
),
54+
);
55+
}
56+
57+
58+
// const preorder = [3, 9, 20, 15, 7];
59+
// const inorder = [9, 3, 15, 20, 7];
60+
// console.log('output:', treeToArray(buildTree(preorder, inorder)));
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""TC: O(n), SC: O(n)
2+
3+
์•„์ด๋””์–ด:
4+
- preorder ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ ธ ์žˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ๋‹ค.
5+
- [root๊ฐ’, [...left], [...right]]
6+
- ์œ„์˜ left, right๋Š” preorder ํŠธ๋ฆฌ์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
7+
- inorder ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ ธ ์žˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ๋‹ค.
8+
- [[...left], root๊ฐ’, [...right]]
9+
- ์œ„์˜ left, right๋Š” inorder ํŠธ๋ฆฌ์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
10+
- ์ด๋•Œ,
11+
- left์˜ ์ฒซ ์•„์ดํ…œ์ด ์ธ๋ฑ์Šค inorder_s์— ์žˆ๊ณ ,
12+
- right์˜ ๋งˆ์ง€๋ง‰ ์•„์ดํ…œ์ด ์ธ๋ฑ์Šค inorder_e - 1์— ์žˆ๋‹ค๊ณ  ํ•˜์ž.
13+
- ์ฆ‰, inorder_e๋ฅผ ๋ฏธํฌํ•จ!
14+
- preorder ํŠธ๋ฆฌ์˜ ๋งจ ์•ž ๊ฐ’์„ ํ†ตํ•ด root๊ฐ’ val์„ ์ฐพ๊ณ , ์ด ๊ฐ’์œผ๋กœ inorder์˜ root๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
15+
- ๋ชจ๋“  node์˜ val๊ฐ’์ด uniqueํ•œ ๊ฒƒ์ด ์กฐ๊ฑด์œผ๋กœ ์ฃผ์–ด์ ธ ์žˆ์œผ๋ฏ€๋กœ val๊ฐ’์˜ indices๋ฅผ ์ „์ฒ˜๋ฆฌํ•ด๋‘˜ ์ˆ˜ ์žˆ๋‹ค.
16+
- ์ด๋•Œ, inorder์˜ root๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ inorder_root์ด๋ผ๊ณ  ํ•˜์ž.
17+
- inorder์˜ root๊ฐ’์˜ ์œ„์น˜์™€ inorder ํŠธ๋ฆฌ์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค๋ฉด
18+
[...left]์˜ ๊ธธ์ด left_len์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
19+
- left_len = inorder_root - inorder_start
20+
- preorder ํŠธ๋ฆฌ์˜ left์˜ ๋ฃจํŠธ๋Š” [...left]์˜ ์ฒซ ์•„์ดํ…œ, ์ฆ‰, preorder_root์— 1์„ ๋”ํ•œ ๊ฐ’์ด๋‹ค.
21+
- preorder ํŠธ๋ฆฌ์˜ right์˜ ๋ฃจํŠธ๋Š” [...right]์˜ ์ฒซ ์•„์ดํ…œ, ์ฆ‰, preorder_root + 1 + left_len์ด๋‹ค.
22+
- root๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์—†์œผ๋ฉด ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค.
23+
- inorder_s >= inorder_e์™€ ๊ฐ™์ด ํŒ๋ณ„์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฆ‰, ์•„์ดํ…œ์ด ํ•˜๋‚˜๋„ ์—†๋Š” ๊ฒฝ์šฐ.
24+
25+
์œ„์˜ ์•„์ด๋””์–ด๋ฅผ ์ข…ํ•ฉํ•˜๋ฉด,
26+
- preorder ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ ์ธ๋ฑ์Šค preorder_root๊ฐ€ ์ฃผ์–ด์ง„, ๊ตฌ๊ฐ„ (inorder_s, inorder_e)์—์„œ ์ •์˜๋œ inorder ํŠธ๋ฆฌ๋Š”
27+
- val๊ฐ’์€ preorder[preorder_root]์ด ๋œ๋‹ค.
28+
- left node๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ•ด์ง„๋‹ค.
29+
- preorder ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ ์ธ๋ฑ์Šค preorder_root + 1,
30+
- ๊ตฌ๊ฐ„ (inorder_s, inorder_root)
31+
- ์ด๋•Œ ๊ตฌ๊ฐ„์ด ์œ ํšจํ•˜์ง€ ์•Š์œผ๋ฉด ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค.
32+
- right node๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ•ด์ง„๋‹ค.
33+
- preorder ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ ์ธ๋ฑ์Šค preorder_root + 1 + left_len,
34+
- ๊ตฌ๊ฐ„ (inorder_root + 1, inorder_end)
35+
- ์ด๋•Œ ๊ตฌ๊ฐ„์ด ์œ ํšจํ•˜์ง€ ์•Š์œผ๋ฉด ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค.
36+
37+
38+
SC:
39+
- ์ฒ˜์Œ inorder_indices๋ฅผ ๊ณ„์‚ฐํ• ๋•Œ O(n).
40+
- ์•„๋ž˜์˜ buildํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ์ตœ๋Œ€ ํŠธ๋ฆฌ์˜ ๊นŠ์ด๋งŒํผ ์žฌ๊ท€๋ฅผ ๋Œ๋ฉด์„œ ์Œ“์ผ ์ˆ˜ ์žˆ๋‹ค.
41+
- ํŠธ๋ฆฌ์˜ ๊นŠ์ด๋Š” ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n).
42+
43+
TC:
44+
- buildํ•จ์ˆ˜๋Š” O(1). ์ฝ”๋“œ ์ฐธ์กฐ.
45+
- ์œ„์˜ ๊ณผ์ •์„ n๊ฐœ์˜ ๋…ธ๋“œ์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•˜๋ฏ€๋กœ O(n).
46+
"""
47+
48+
49+
# Definition for a binary tree node.
50+
# class TreeNode:
51+
# def __init__(self, val=0, left=None, right=None):
52+
# self.val = val
53+
# self.left = left
54+
# self.right = right
55+
class Solution:
56+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
57+
inorder_indices = {v: i for i, v in enumerate(inorder)}
58+
59+
def build(inorder_s, inorder_e, preorder_root):
60+
if inorder_s >= inorder_e: # O(1)
61+
return None
62+
val = preorder[preorder_root] # O(1)
63+
inorder_root = inorder_indices[val] # O(1)
64+
left_len = inorder_root - inorder_s # O(1)
65+
return TreeNode(
66+
val,
67+
left=build(inorder_s, inorder_root, preorder_root + 1),
68+
right=build(inorder_root + 1, inorder_e, preorder_root + 1 + left_len),
69+
)
70+
71+
return build(0, len(inorder), 0)
72+
73+
74+
"""
75+
๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ ์•„์ด๋””์–ด๋ฅผ ๋‹ค์‹œ ์ƒ๊ฐํ•ด๋ณด๋ฉด, ๋ชจ๋“  ๋…ธ๋“œ๋“ค์„ preorder ์ˆœ์„œ๋กœ ์ˆœํšŒํ•œ๋‹ค!
76+
- `val = preorder[preorder_root]`์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ val๊ฐ’์„ ๊ตฌํ•˜์ง€ ์•Š๊ณ , ์ฃผ์–ด์ง„ preorder๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๊ฐ€์ ธ์™€๋„ ๋จ.
77+
- ์ฆ‰, preorder๋ฅผ iterator๋กœ ๋ฐ”๊ฟ”์„œ next๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๋ฝ‘์•„์™€์„œ ๊ฑด๋„ค์ค˜๋„ ๋œ๋‹ค.
78+
- ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด buildํ•จ์ˆ˜์— preorder_root๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์•„๋„ ๋จ.
79+
"""
80+
81+
82+
class Solution:
83+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
84+
inorder_indices = {v: i for i, v in enumerate(inorder)}
85+
preorder_iter = iter(preorder)
86+
87+
def build(inorder_s, inorder_e):
88+
if inorder_s >= inorder_e: # O(1)
89+
return None
90+
val = next(preorder_iter) # O(1)
91+
inorder_root = inorder_indices[val] # O(1)
92+
return TreeNode(
93+
val,
94+
left=build(inorder_s, inorder_root),
95+
right=build(inorder_root + 1, inorder_e),
96+
)
97+
98+
return build(0, len(inorder))

0 commit comments

Comments
ย (0)