Skip to content

Commit c2b493d

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 22fc8f9 + 2d8c098 commit c2b493d

File tree

99 files changed

+3506
-2
lines changed

Some content is hidden

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

99 files changed

+3506
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class TreeNode {
2+
val: number
3+
left: TreeNode | null
4+
right: TreeNode | null
5+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
this.val = (val===undefined ? 0 : val)
7+
this.left = (left===undefined ? null : left)
8+
this.right = (right===undefined ? null : right)
9+
}
10+
}
11+
12+
13+
// TC: O(n)
14+
// SC: O(n)
15+
function levelOrder(root: TreeNode | null): number[][] {
16+
if (!root) return [];
17+
18+
const result: number[][] = [];
19+
const queue: TreeNode[] = [root];
20+
21+
while (queue.length > 0) {
22+
const values: number[] = [];
23+
const level = queue.length;
24+
25+
for (let i = 0; i < level; i++) {
26+
const node = queue.shift()!;
27+
values.push(node.val);
28+
29+
if (node.left) {
30+
queue.push(node.left);
31+
}
32+
33+
if (node.right) {
34+
queue.push(node.right);
35+
}
36+
}
37+
38+
result.push(values);
39+
}
40+
41+
return result;
42+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import java.util.ArrayList;
2+
import java.util.LinkedList;
3+
import java.util.List;
4+
import java.util.Queue;
5+
6+
/**
7+
* Definition for a binary tree node.
8+
* public class TreeNode {
9+
* int val;
10+
* TreeNode left;
11+
* TreeNode right;
12+
* TreeNode() {}
13+
* TreeNode(int val) { this.val = val; }
14+
* TreeNode(int val, TreeNode left, TreeNode right) {
15+
* this.val = val;
16+
* this.left = left;
17+
* this.right = right;
18+
* }
19+
* }
20+
*/
21+
class Solution {
22+
23+
// DFS 풀이
24+
public List<List<Integer>> levelOrder(TreeNode root) {
25+
List<List<Integer>> result = new ArrayList<>();
26+
dfs(root, 0, result);
27+
return result;
28+
}
29+
30+
private void dfs(TreeNode node, int depth, List<List<Integer>> result) {
31+
if (node == null) {
32+
return;
33+
}
34+
35+
// 깊이만큼의 λ¦¬μŠ€νŠΈκ°€ μ—†μœΌλ©΄ μƒˆ 리슀트 μΆ”κ°€ν•˜κΈ°
36+
if (depth == result.size()) {
37+
result.add(new ArrayList<>());
38+
}
39+
40+
result.get(depth).add(node.val);
41+
42+
dfs(node.left, depth + 1, result);
43+
dfs(node.right, depth + 1, result);
44+
45+
46+
}
47+
48+
// BFS둜 풀이
49+
// O(n)
50+
// public List<List<Integer>> levelOrder(TreeNode root) {
51+
//
52+
// List<List<Integer>> result = new ArrayList<>();
53+
//
54+
// if (root == null) {
55+
// return result;
56+
// }
57+
//
58+
// Queue<TreeNode> queue = new LinkedList<>();
59+
// queue.offer(root);
60+
//
61+
// while (!queue.isEmpty()) {
62+
// int nodeCnt = queue.size();
63+
// List<Integer> currentLevelNodes = new ArrayList<>();
64+
//
65+
// for (int i = 0; i < nodeCnt; i++) {
66+
// TreeNode current = queue.poll();
67+
// currentLevelNodes.add(current.val);
68+
//
69+
// if (current.left != null) {
70+
// queue.offer(current.left);
71+
// }
72+
//
73+
// if (current.right != null) {
74+
// queue.offer(current.right);
75+
// }
76+
//
77+
// }
78+
//
79+
// result.add(currentLevelNodes);
80+
//
81+
// }
82+
//
83+
// return result;
84+
//
85+
// }
86+
}
87+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def levelOrder(self, root):
3+
result = []
4+
def dfs(node, level):
5+
if not node:
6+
return
7+
if len(result) == level:
8+
result.append([])
9+
result[level].append(node.val)
10+
dfs(node.left, level + 1)
11+
dfs(node.right, level + 1)
12+
dfs(root, 0)
13+
return result
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
[λ¬Έμ œν’€μ΄]
3+
# Inputs
4+
5+
# Outputs
6+
7+
# Constraints
8+
9+
# Ideas
10+
11+
[회고]
12+
13+
"""
14+
15+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
8+
from collections import deque
9+
10+
class Solution:
11+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
12+
13+
# BFS(큐 μ‚¬μš©)
14+
# μ‹œκ°„λ³΅μž‘λ„ O(n), κ³΅κ°„λ³΅μž‘λ„ O(n)
15+
16+
if not root:
17+
return []
18+
19+
# 큐에 rootλ…Έλ“œ μΆ”κ°€(μ΄ˆκΈ°ν™”)
20+
q = deque([root])
21+
answer = []
22+
23+
# 큐가 빌 λ•ŒκΉŒμ§€ 탐색(λͺ¨λ“  λ…Έλ“œ 탐색)
24+
while q:
25+
level = [] # ν˜„μž¬ 레벨의 λ…Έλ“œ μ €μž₯ν•  리슀트
26+
27+
for _ in range(len(q)): # ν˜„μž¬ λ ˆλ²¨μ— μžˆλŠ” λ…Έλ“œ 수만큼 반볡
28+
node = q.popleft() # νμ—μ„œ λ…Έλ“œ κΊΌλ‚΄μ„œ
29+
level.append(node.val) # level에 μ €μž₯
30+
31+
# μžμ‹ λ…Έλ“œλ“€μ΄ μžˆλ‹€λ©΄ 큐에 μΆ”κ°€
32+
if node.left:
33+
q.append(node.left)
34+
if node.right:
35+
q.append(node.right)
36+
37+
# ν˜„μž¬ 레벨 answer에 μΆ”κ°€
38+
answer.append(level)
39+
40+
return answer

β€Žcounting-bits/Jeehay28.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TC: O(n)
2+
// SC: O(n)
3+
function countBits(n: number): number[] {
4+
const dp: number[] = [];
5+
dp[0] = 0;
6+
7+
for (let i = 0; i <= n; i++) {
8+
dp[i] = dp[i >> 1] + (i & 1);
9+
}
10+
11+
// The number of 1s in the quotient (i >> 1) + number of 1s in the remainder (i & 1)
12+
// dp[i >> 1]: number of 1's in Math.floor(i / 2)
13+
// i & 1: 1 if i is odd, 0 if even
14+
15+
return dp;
16+
}
17+

β€Žcounting-bits/Tessa1217.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
3+
// μ‹œκ°„λ³΅μž‘λ„: O(n)
4+
public int[] countBits(int n) {
5+
int[] bitsArray = new int[n + 1];
6+
for (int i = 1; i <= n; i++) {
7+
// Shift μ—°μ‚°μž μ‚¬μš©
8+
// i&1 => λ§ˆμ§€λ§‰ λΉ„νŠΈκ°€ 0인지 1인지 확인
9+
bitsArray[i] = bitsArray[i >> 1] + (i & 1);
10+
}
11+
return bitsArray;
12+
}
13+
}
14+

β€Žcounting-bits/byol-han.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* https://leetcode.com/problems/counting-bits/submissions/1681218194/
3+
* @param {number} n
4+
* @return {number[]}
5+
*/
6+
var countBits = function (n) {
7+
const ans = [];
8+
for (let i = 0; i <= n; i++) {
9+
// Convert i to binary with i.toString(2)
10+
// Count 1s by splitting into chars, filtering '1', and getting length
11+
const binary = i.toString(2);
12+
const onesCount = binary.split('').filter((bit) => bit === '1').length;
13+
ans.push(onesCount);
14+
}
15+
return ans;
16+
};

β€Žcounting-bits/eunhwa99.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
/*
3+
0 -> 0
4+
1 -> 1
5+
2 -> 1
6+
3 -> 2
7+
4 -> 1
8+
5 -> 2
9+
6 -> 2
10+
7 -> 3
11+
8 -> 1
12+
9 -> 2
13+
10 -> 2
14+
11 -> 3
15+
12 -> 2
16+
2,4,8,16,.. -> 1
17+
짝수: i/2의 1의 개수
18+
ν™€μˆ˜: i/2의 1의 개수 + 1
19+
*/
20+
// TC: O(n)
21+
// SC: O(n)
22+
fun countBits(n: Int): IntArray {
23+
val result = IntArray(n + 1)
24+
for(i in 0..n) {
25+
result[i] = result[i / 2] + (i % 2)
26+
}
27+
return result
28+
}
29+
}

β€Žcounting-bits/printjin-gmailcom.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def countBits(self, n):
3+
ans = [0] * (n + 1)
4+
for i in range(1, n + 1):
5+
ans[i] = ans[i >> 1] + (i & 1)
6+
return ans

0 commit comments

Comments
Β (0)