File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
maximum-depth-of-binary-tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def maxDepth (self , root : Optional [TreeNode ]) -> int :
3
+ if not root :
4
+ return 0
5
+
6
+ right = self .maxDepth (root .right )
7
+ left = self .maxDepth (root .left )
8
+
9
+ return max (right , left ) + 1
Original file line number Diff line number Diff line change
1
+ # class ListNode:
2
+ # def __init__(self, val=0, next=None):
3
+ # self.val = val
4
+ # self.next = next
5
+
6
+
7
+ class Solution :
8
+ def mergeTwoLists (self , list1 : Optional [ListNode ], list2 : Optional [ListNode ]) -> Optional [ListNode ]:
9
+ if not list1 :
10
+ return list2
11
+
12
+ if not list2 :
13
+ return list1
14
+
15
+ merge_node : ListNode = ListNode ()
16
+ current = merge_node
17
+
18
+ while list1 and list2 :
19
+ if list1 .val < list2 .val :
20
+ current .next = list1
21
+ list1 = list1 .next
22
+ else :
23
+ current .next = list2
24
+ list2 = list2 .next
25
+
26
+ current = current .next
27
+
28
+ current .next = list1 or list2
29
+ return merge_node .next
You can’t perform that action at this time.
0 commit comments