Skip to content

Commit 2a0fd57

Browse files
authored
Merge pull request #2474 from grapefruit13/main
2 parents 20a3d34 + a651ff5 commit 2a0fd57

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class TreeNode {
2+
val: number;
3+
left: TreeNode | null;
4+
right: TreeNode | null;
5+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.left = left === undefined ? null : left;
8+
this.right = right === undefined ? null : right;
9+
}
10+
}
11+
12+
/**
13+
* Time Complexity: O(n)
14+
* Space Complexity: O(h)
15+
*/
16+
function maxDepth(root: TreeNode | null): number {
17+
if (root === null) return 0;
18+
19+
let left = maxDepth(root.left);
20+
let right = maxDepth(root.right);
21+
22+
return 1 + Math.max(left, right);
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = val === undefined ? 0 : val;
6+
this.next = next === undefined ? null : next;
7+
}
8+
}
9+
10+
/**
11+
* Time Complexity: O(n + m)
12+
* Space Complexity: O(1)
13+
*/
14+
function mergeTwoLists(
15+
list1: ListNode | null,
16+
list2: ListNode | null
17+
): ListNode | null {
18+
const dummy = new ListNode(0);
19+
let current = dummy;
20+
21+
while (list1 && list2) {
22+
if (list1.val < list2.val) {
23+
current.next = list1;
24+
list1 = list1.next;
25+
} else {
26+
current.next = list2;
27+
list2 = list2.next;
28+
}
29+
current = current.next;
30+
}
31+
current.next = list1 || list2;
32+
return dummy.next;
33+
}

0 commit comments

Comments
 (0)