From 053f8c9ee1dab980be4b7d7facc9ae696fd3da0c Mon Sep 17 00:00:00 2001 From: RiaOh Date: Wed, 23 Apr 2025 23:27:25 +0900 Subject: [PATCH 1/2] merge-tw-sorted-lists solution --- merge-two-sorted-lists/RiaOh.js | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 merge-two-sorted-lists/RiaOh.js diff --git a/merge-two-sorted-lists/RiaOh.js b/merge-two-sorted-lists/RiaOh.js new file mode 100644 index 000000000..978f23a57 --- /dev/null +++ b/merge-two-sorted-lists/RiaOh.js @@ -0,0 +1,34 @@ +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} list1 + * @param {ListNode} list2 + * @return {ListNode} + */ +var mergeTwoLists = function (list1, list2) { + const node = new ListNode(); + let tail = node; + + while (list1 && list2) { + if (list1.val < list2.val) { + tail.next = list1; + list1 = list1.next; + } else { + tail.next = list2; + list2 = list2.next; + } + tail = tail.next; + } + + if (list2) { + tail.next = list2; + } else if (list1) { + tail.next = list1; + } + return node.next; +}; From 78cdc6a8cc41f487644d4d4b666b53d18c054f85 Mon Sep 17 00:00:00 2001 From: RiaOh Date: Thu, 24 Apr 2025 23:54:08 +0900 Subject: [PATCH 2/2] maximum-depth-of-binary-tree solution --- maximum-depth-of-binary-tree/RiaOh.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 maximum-depth-of-binary-tree/RiaOh.js diff --git a/maximum-depth-of-binary-tree/RiaOh.js b/maximum-depth-of-binary-tree/RiaOh.js new file mode 100644 index 000000000..67d89ab83 --- /dev/null +++ b/maximum-depth-of-binary-tree/RiaOh.js @@ -0,0 +1,22 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var maxDepth = function (root) { + if (root === null) { + return 0; + } else { + const leftDepth = maxDepth(root.left); + const rightDepth = maxDepth(root.right); + + return Math.max(leftDepth, rightDepth) + 1; + } +};