Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions maximum-depth-of-binary-tree/i-mprovising.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Time complexity O(n)
"""
from collections import deque

class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0

max_depth = 0

# bfs
q = deque([[root, 1]])
while(q):
node, depth = q.pop()
if max_depth < depth:
max_depth = depth
if node.left:
q.append([node.left, depth+1])
if node.right:
q.append([node.right, depth+1])

return max_depth
35 changes: 35 additions & 0 deletions merge-two-sorted-lists/i-mprovising.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Time complexity O(n+m)
"""

class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
if not (list1 or list2):
return list1 or list2 # if empty list

nums = []
while(list1 or list2):
if not list1:
val = list2.val
list2 = list2.next
elif not list2:
val = list1.val
list1 = list1.next
else:
if list1.val <= list2.val:
val = list1.val
list1 = list1.next
else:
val = list2.val
list2 = list2.next
nums.append(val)

head = ListNode(nums[0])
node = head
if len(nums) == 1:
return head
for n in nums[1:]:
tmp = ListNode(n)
node.next = tmp
node = tmp
return head