Skip to content

[YoungSeok-Choi] Week 4 Solutions #1330

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions maximum-depth-of-binary-tree/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.LinkedList;
import java.util.Queue;

// 시간복잡도 O(n)
class Solution {
public int depth = 0;
public int maxDepth(TreeNode root) {

if(root == null) {
return depth;
}

Queue<TreeNode> q = new LinkedList<>();
q.add(root);

while(!q.isEmpty()) {
int size = q.size();
depth++;

for(int i = 0; i < size; i++) {
TreeNode p = q.poll();

if(p.right != null) {
q.add(p.right);
}

if(p.left != null) {
q.add(p.left);
}
}
}

return depth;
}
}
44 changes: 44 additions & 0 deletions merge-two-sorted-lists/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 시간복잡도 O(N + M)
class Solution {

public ListNode root = null;
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {

while(list1 != null || list2 != null) {
int v1 = 9999;
int v2 = 9999;

if(list1 != null) {
v1 = list1.val;
}

if(list2 != null) {
v2 = list2.val;
}

if(v1 < v2) {
addNode(v1);
list1 = list1.next;
} else {
addNode(v2);
list2 = list2.next;
}
}

return root;
}

public void addNode (int val) {
if(root == null) {
root = new ListNode(val);
return;
}

ListNode now = root;
while(now.next != null) {
now = now.next;
}

now.next = new ListNode(val);
}
}