diff --git a/binary-tree-level-order-traversal/dev-jonghoonpark.md b/binary-tree-level-order-traversal/dev-jonghoonpark.md new file mode 100644 index 000000000..9e5fbe654 --- /dev/null +++ b/binary-tree-level-order-traversal/dev-jonghoonpark.md @@ -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> levelOrder(TreeNode root) { + List> result = new ArrayList<>(); + + dfs(result, root, 0); + + return result; + } + + public void dfs(List> 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); + } +} +``` diff --git a/lowest-common-ancestor-of-a-binary-search-tree/dev-jonghoonpark.md b/lowest-common-ancestor-of-a-binary-search-tree/dev-jonghoonpark.md new file mode 100644 index 000000000..23b4d0156 --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/dev-jonghoonpark.md @@ -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; + } +} +``` diff --git a/remove-nth-node-from-end-of-list/dev-jonghoonpark.md b/remove-nth-node-from-end-of-list/dev-jonghoonpark.md new file mode 100644 index 000000000..91407b300 --- /dev/null +++ b/remove-nth-node-from-end-of-list/dev-jonghoonpark.md @@ -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; + } +} +``` diff --git a/reorder-list/dev-jonghoonpark.md b/reorder-list/dev-jonghoonpark.md new file mode 100644 index 000000000..0bb1dc682 --- /dev/null +++ b/reorder-list/dev-jonghoonpark.md @@ -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 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; + } +} +``` diff --git a/validate-binary-search-tree/dev-jonghoonpark.md b/validate-binary-search-tree/dev-jonghoonpark.md new file mode 100644 index 000000000..cf96286bf --- /dev/null +++ b/validate-binary-search-tree/dev-jonghoonpark.md @@ -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; + } +} +```