Skip to content

Commit 713c831

Browse files
week7 mission done
1 parent 5379515 commit 713c831

File tree

5 files changed

+211
-0
lines changed

5 files changed

+211
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
- 문제: https://leetcode.com/problems/binary-tree-level-order-traversal/
2+
- time complexity : O(n)
3+
- space complexity : O(n), 트리가 균등한 형태일 경우 O(logn)에 가까워진다. (결과에 사용되는 list는 고려하지 않는다.)
4+
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-102
5+
6+
```java
7+
class Solution {
8+
public List<List<Integer>> levelOrder(TreeNode root) {
9+
List<List<Integer>> result = new ArrayList<>();
10+
11+
dfs(result, root, 0);
12+
13+
return result;
14+
}
15+
16+
public void dfs(List<List<Integer>> result, TreeNode node, int level) {
17+
if(node == null) {
18+
return;
19+
}
20+
21+
if (result.size() <= level) {
22+
result.add(new ArrayList<>());
23+
}
24+
result.get(level).add(node.val);
25+
26+
dfs(result, node.left, level + 1);
27+
dfs(result, node.right, level + 1);
28+
}
29+
}
30+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
- 문제: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
2+
- time complexity : O(n)
3+
- space complexity : O(n), 트리가 균등한 형태일 경우 O(logn)에 가까워진다.
4+
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-235
5+
6+
```java
7+
class Solution {
8+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
9+
return dfs(root, p, q);
10+
}
11+
12+
private TreeNode dfs(TreeNode node, TreeNode p, TreeNode q) {
13+
if (node == null) {
14+
return null;
15+
}
16+
17+
TreeNode left = dfs(node.left, p, q);
18+
TreeNode right = dfs(node.right, p, q);
19+
20+
if ((left != null && right != null) || node.val == p.val || node.val == q.val) {
21+
return node;
22+
}
23+
24+
return left != null ? left : right;
25+
}
26+
}
27+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
- 문제: https://leetcode.com/problems/remove-nth-node-from-end-of-list/
2+
- time complexity : O(n)
3+
- space complexity : O(1)
4+
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-19
5+
6+
```java
7+
class Solution {
8+
public ListNode removeNthFromEnd(ListNode head, int n) {
9+
ListNode current = head;
10+
int length = 0;
11+
while(current != null) {
12+
length++;
13+
current = current.next;
14+
}
15+
16+
if (length == 1) {
17+
return null;
18+
}
19+
20+
if (length == n) {
21+
return head.next;
22+
}
23+
24+
int removeIndex = length - n;
25+
int pointer = 0;
26+
current = head;
27+
while (pointer < removeIndex - 1) {
28+
current = current.next;
29+
pointer++;
30+
}
31+
current.next = current.next.next;
32+
return head;
33+
}
34+
}
35+
```

reorder-list/dev-jonghoonpark.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
- 문제 : https://leetcode.com/problems/reorder-list/
2+
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-143
3+
4+
## 방법 1 (list 사용)
5+
6+
- time complexity : O(n)
7+
- space complexity : O(n)
8+
9+
```java
10+
class Solution {
11+
public void reorderList(ListNode head) {
12+
List<ListNode> list = new ArrayList<>();
13+
14+
ListNode current = head;
15+
while (current.next != null) {
16+
list.add(current);
17+
current = current.next;
18+
}
19+
list.add(current);
20+
21+
ListNode reordered = head;
22+
for (int i = 1; i < list.size(); i++) {
23+
if (i % 2 == 0) {
24+
reordered.next = list.get(i / 2);
25+
} else {
26+
reordered.next = list.get(list.size() - ((i / 2) + 1));
27+
}
28+
reordered = reordered.next;
29+
}
30+
reordered.next = null;
31+
}
32+
}
33+
```
34+
35+
## 방법 2 (pointer 사용)
36+
37+
- time complexity : O(n)
38+
- space complexity : O(1)
39+
40+
```java
41+
class Solution {
42+
public void reorderList(ListNode head) {
43+
ListNode prev = null;
44+
ListNode slow = head;
45+
ListNode fast = head;
46+
47+
while(slow != null && fast != null && fast.next != null) {
48+
prev = slow;
49+
slow = slow.next;
50+
fast = fast.next.next;
51+
}
52+
53+
if (prev == null) {
54+
return;
55+
}
56+
57+
prev.next = null;
58+
59+
ListNode current = head;
60+
ListNode reversed = reverse(slow);
61+
while(true) {
62+
ListNode temp = current.next;
63+
64+
if (reversed != null) {
65+
current.next = reversed;
66+
reversed = reversed.next;
67+
current = current.next;
68+
}
69+
70+
if (temp != null) {
71+
current.next = temp;
72+
current = current.next;
73+
} else {
74+
current.next = reversed;
75+
break;
76+
}
77+
}
78+
}
79+
80+
public ListNode reverse(ListNode treeNode) {
81+
ListNode current = treeNode;
82+
ListNode prev = null;
83+
while(current != null) {
84+
ListNode temp = current.next;
85+
current.next = prev;
86+
prev = current;
87+
current = temp;
88+
}
89+
return prev;
90+
}
91+
}
92+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
- 문제 : https://leetcode.com/problems/validate-binary-search-tree/
2+
- time complexity : O(n)
3+
- space complexity : O(n), 트리가 균등한 형태일 경우 O(logn)에 가까워진다.
4+
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-98
5+
6+
```java
7+
class Solution {
8+
public boolean isValidBST(TreeNode root) {
9+
return dfs(root.left, (long) Integer.MIN_VALUE - 1, root.val)
10+
&& dfs(root.right, root.val, (long) Integer.MAX_VALUE + 1);
11+
}
12+
13+
private boolean dfs(TreeNode node, long min, long max) {
14+
if (node == null) {
15+
return true;
16+
}
17+
18+
// left로 갈 때 max를 자신으로, right로 갈 때는 min을 자신으로
19+
if (!(dfs(node.left, min, node.val)
20+
&& dfs(node.right, node.val, max))) {
21+
return false;
22+
}
23+
24+
return node.val > min && node.val < max;
25+
}
26+
}
27+
```

0 commit comments

Comments
 (0)