Skip to content

Commit 3629dbe

Browse files
committed
WEEK 04 Solutions
1 parent c8d52f3 commit 3629dbe

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public int maxDepth(TreeNode root) {
18+
if (root == null) {
19+
return 0;
20+
}
21+
22+
int leftDepth = maxDepth(root.left);
23+
int rightDepth = maxDepth(root.right);
24+
25+
return Math.max(leftDepth, rightDepth) +1;
26+
}
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
13+
ListNode dummy = new ListNode(0);
14+
ListNode current = dummy;
15+
16+
while(list1 != null && list2 != null) {
17+
if (list1.val <= list2.val) {
18+
current.next = list1;
19+
list1 = list1.next;
20+
} else {
21+
current.next = list2;
22+
list2 = list2.next;
23+
}
24+
current = current.next;
25+
}
26+
27+
if (list1 != null) {
28+
current.next = list1;
29+
} else {
30+
current.next = list2;
31+
}
32+
33+
return dummy.next;
34+
}
35+
}

0 commit comments

Comments
 (0)