diff --git a/maximum-depth-of-binary-tree/YoungSeok-Choi.java b/maximum-depth-of-binary-tree/YoungSeok-Choi.java new file mode 100644 index 000000000..77b892b48 --- /dev/null +++ b/maximum-depth-of-binary-tree/YoungSeok-Choi.java @@ -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 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; + } +} diff --git a/merge-two-sorted-lists/YoungSeok-Choi.java b/merge-two-sorted-lists/YoungSeok-Choi.java new file mode 100644 index 000000000..10164c9ef --- /dev/null +++ b/merge-two-sorted-lists/YoungSeok-Choi.java @@ -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); + } +}