From 13adfecd33d7dab83c507b66f93537d56c6aa333 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Sun, 20 Apr 2025 09:22:10 +0900 Subject: [PATCH 1/2] add: merge-two sorted lists --- merge-two-sorted-lists/YoungSeok-Choi.java | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 merge-two-sorted-lists/YoungSeok-Choi.java 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); + } +} From 9efe879d5121333b872e08f9cb17ae37e8bc0bf3 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Sun, 20 Apr 2025 09:53:49 +0900 Subject: [PATCH 2/2] add: Maximum depth of binary tree --- .../YoungSeok-Choi.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 maximum-depth-of-binary-tree/YoungSeok-Choi.java 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; + } +}