-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5379515
commit 713c831
Showing
5 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
- 문제: https://leetcode.com/problems/binary-tree-level-order-traversal/ | ||
- time complexity : O(n) | ||
- space complexity : O(n), 트리가 균등한 형태일 경우 O(logn)에 가까워진다. (결과에 사용되는 list는 고려하지 않는다.) | ||
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-102 | ||
|
||
```java | ||
class Solution { | ||
public List<List<Integer>> levelOrder(TreeNode root) { | ||
List<List<Integer>> result = new ArrayList<>(); | ||
|
||
dfs(result, root, 0); | ||
|
||
return result; | ||
} | ||
|
||
public void dfs(List<List<Integer>> result, TreeNode node, int level) { | ||
if(node == null) { | ||
return; | ||
} | ||
|
||
if (result.size() <= level) { | ||
result.add(new ArrayList<>()); | ||
} | ||
result.get(level).add(node.val); | ||
|
||
dfs(result, node.left, level + 1); | ||
dfs(result, node.right, level + 1); | ||
} | ||
} | ||
``` |
27 changes: 27 additions & 0 deletions
27
lowest-common-ancestor-of-a-binary-search-tree/dev-jonghoonpark.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
- 문제: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ | ||
- time complexity : O(n) | ||
- space complexity : O(n), 트리가 균등한 형태일 경우 O(logn)에 가까워진다. | ||
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-235 | ||
|
||
```java | ||
class Solution { | ||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | ||
return dfs(root, p, q); | ||
} | ||
|
||
private TreeNode dfs(TreeNode node, TreeNode p, TreeNode q) { | ||
if (node == null) { | ||
return null; | ||
} | ||
|
||
TreeNode left = dfs(node.left, p, q); | ||
TreeNode right = dfs(node.right, p, q); | ||
|
||
if ((left != null && right != null) || node.val == p.val || node.val == q.val) { | ||
return node; | ||
} | ||
|
||
return left != null ? left : right; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
- 문제: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ | ||
- time complexity : O(n) | ||
- space complexity : O(1) | ||
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-19 | ||
|
||
```java | ||
class Solution { | ||
public ListNode removeNthFromEnd(ListNode head, int n) { | ||
ListNode current = head; | ||
int length = 0; | ||
while(current != null) { | ||
length++; | ||
current = current.next; | ||
} | ||
|
||
if (length == 1) { | ||
return null; | ||
} | ||
|
||
if (length == n) { | ||
return head.next; | ||
} | ||
|
||
int removeIndex = length - n; | ||
int pointer = 0; | ||
current = head; | ||
while (pointer < removeIndex - 1) { | ||
current = current.next; | ||
pointer++; | ||
} | ||
current.next = current.next.next; | ||
return head; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
- 문제 : https://leetcode.com/problems/reorder-list/ | ||
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-143 | ||
|
||
## 방법 1 (list 사용) | ||
|
||
- time complexity : O(n) | ||
- space complexity : O(n) | ||
|
||
```java | ||
class Solution { | ||
public void reorderList(ListNode head) { | ||
List<ListNode> list = new ArrayList<>(); | ||
|
||
ListNode current = head; | ||
while (current.next != null) { | ||
list.add(current); | ||
current = current.next; | ||
} | ||
list.add(current); | ||
|
||
ListNode reordered = head; | ||
for (int i = 1; i < list.size(); i++) { | ||
if (i % 2 == 0) { | ||
reordered.next = list.get(i / 2); | ||
} else { | ||
reordered.next = list.get(list.size() - ((i / 2) + 1)); | ||
} | ||
reordered = reordered.next; | ||
} | ||
reordered.next = null; | ||
} | ||
} | ||
``` | ||
|
||
## 방법 2 (pointer 사용) | ||
|
||
- time complexity : O(n) | ||
- space complexity : O(1) | ||
|
||
```java | ||
class Solution { | ||
public void reorderList(ListNode head) { | ||
ListNode prev = null; | ||
ListNode slow = head; | ||
ListNode fast = head; | ||
|
||
while(slow != null && fast != null && fast.next != null) { | ||
prev = slow; | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
} | ||
|
||
if (prev == null) { | ||
return; | ||
} | ||
|
||
prev.next = null; | ||
|
||
ListNode current = head; | ||
ListNode reversed = reverse(slow); | ||
while(true) { | ||
ListNode temp = current.next; | ||
|
||
if (reversed != null) { | ||
current.next = reversed; | ||
reversed = reversed.next; | ||
current = current.next; | ||
} | ||
|
||
if (temp != null) { | ||
current.next = temp; | ||
current = current.next; | ||
} else { | ||
current.next = reversed; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public ListNode reverse(ListNode treeNode) { | ||
ListNode current = treeNode; | ||
ListNode prev = null; | ||
while(current != null) { | ||
ListNode temp = current.next; | ||
current.next = prev; | ||
prev = current; | ||
current = temp; | ||
} | ||
return prev; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
- 문제 : https://leetcode.com/problems/validate-binary-search-tree/ | ||
- time complexity : O(n) | ||
- space complexity : O(n), 트리가 균등한 형태일 경우 O(logn)에 가까워진다. | ||
- 블로그 링크 : https://algorithm.jonghoonpark.com/2024/06/10/leetcode-98 | ||
|
||
```java | ||
class Solution { | ||
public boolean isValidBST(TreeNode root) { | ||
return dfs(root.left, (long) Integer.MIN_VALUE - 1, root.val) | ||
&& dfs(root.right, root.val, (long) Integer.MAX_VALUE + 1); | ||
} | ||
|
||
private boolean dfs(TreeNode node, long min, long max) { | ||
if (node == null) { | ||
return true; | ||
} | ||
|
||
// left로 갈 때 max를 자신으로, right로 갈 때는 min을 자신으로 | ||
if (!(dfs(node.left, min, node.val) | ||
&& dfs(node.right, node.val, max))) { | ||
return false; | ||
} | ||
|
||
return node.val > min && node.val < max; | ||
} | ||
} | ||
``` |