Skip to content

[jaejeong1] WEEK 11 Solutions #551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions invert-binary-tree/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// 풀이: child node로 내려가면서 재귀로 계속 left와 right를 바꿔준다.
// TC: O(N)
// SC: O(N)
public TreeNode invertTree(TreeNode root) {
return invert(root);
}

private TreeNode invert(TreeNode node) {
if (node == null) {
return node;
}

node = swap(node);

invert(node.left);
invert(node.right);

return node;
}

private TreeNode swap(TreeNode node) {
var temp = node.left;
node.left = node.right;
node.right = temp;

return node;
}
}
41 changes: 41 additions & 0 deletions maximum-depth-of-binary-tree/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// 풀이 : DFS 탐색을 통해 리프 노드까지 탐색하면서 깊이를 계산한다.
// TC: O(N), SC: O(N)
int answer = 0;

public int maxDepth(TreeNode root) {
dfs(root, 1);

return answer;
}

private void dfs(TreeNode node, int depth) {
if (node == null) {
return;
}

if (depth > answer) {
answer = depth;
}

depth++;

dfs(node.left, depth);
dfs(node.right, depth);
}
}
54 changes: 54 additions & 0 deletions reorder-list/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.Stack;

// Definition for singly-linked list.
class ListNode {

int val;
ListNode next;

ListNode() {
}

ListNode(int val) {
this.val = val;
}

ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}

class Solution {

public void reorderList(ListNode head) {
// 풀이: 역순으로 저장할 스택에 node들을 넣고, 기존 node 1개/스택 node 1개씩 이어 붙인다
// 스택은 LIFO로 저장되기 때문에, 문제에서 요구하는 순서대로 reorderList를 만들 수 있다
// TC: O(N)
// SC: O(N)
Stack<ListNode> stack = new Stack<>();

var curNode = head;
while(curNode != null) {
stack.push(curNode);
curNode = curNode.next;
}

curNode = head;
var halfSize = stack.size() / 2; // 한번에 2개씩 연결하기때문에 절반까지만 돌면 됨
for (int i=0; i<halfSize; i++) {
var top = stack.pop();

var nextNode = curNode.next;
curNode.next = top;
top.next = nextNode;

curNode = nextNode;
}

// 만약 노드의 개수가 홀수면, 하나의 노드가 남기 때문에 next 노드를 null로 처리해줘야 한다.
if (curNode != null) {
curNode.next = null;
}
}
}