# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: definorderTraversal(self, root: TreeNode) -> List[int]: out = []
definorder(root): ifnot root: return
inorder(root.left) out.append(root.val) inorder(root.right) inorder(root) return out
144 Easy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: defpreorderTraversal(self, root: TreeNode) -> List[int]: out = [] defpreorder(node): ifnot node: return out.append(node.val) preorder(node.left) preorder(node.right) preorder(root) return out
145 Easy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: defpostorderTraversal(self, root: TreeNode) -> List[int]: out = [] defpostorder(node): ifnot node: return postorder(node.left) postorder(node.right) out.append(node.val) postorder(root) return out
226 Easy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: definvertTree(self, root: TreeNode) -> TreeNode:
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: deflevelOrderBottom(self, root: TreeNode) -> List[List[int]]: ifnot root: return []
q = [root] out = [[root.val]] next_level = [] while q: curr_node = q.pop(0) if curr_node.left: next_level.append(curr_node.left) if curr_node.right: next_level.append(curr_node.right) ifnot q: if next_level: temp = [] for i inrange(len(next_level)): temp.append(next_level[i].val) out.insert(0, temp)
""" # Definition for a Node. class Node: def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): self.val = val self.left = left self.right = right self.next = next """
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: defsumNumbers(self, root: TreeNode) -> int: out = 0
deffind_sums(node, num): nonlocal out ifnot node: return ifnot node.left andnot node.right: out += int(num + str(node.val)) find_sums(node.left, num + str(node.val)) find_sums(node.right, num + str(node.val)) find_sums(root, '')
return out
108 Easy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: defsortedArrayToBST(self, nums: List[int]) -> TreeNode: defbuild_tree(subset): ifnot subset: return l, r = 0, len(subset) - 1 mid = l + (l + r) // 2 curr_node = TreeNode(val=subset[mid], left=build_tree(subset[:mid]), right=build_tree(subset[mid+1:])) return curr_node
classSolution: deffrequencySort(self, s: str) -> str: count_dict = {} for i in s: if i notin count_dict.keys(): count_dict[i] = -1 else: count_dict[i] -= 1 temp_lst = [] for i in s: temp_lst.append((count_dict[i], i)) import heapq heapq.heapify(temp_lst) out_str = '' for i inrange(len(s)): out_str += heapq.heappop(temp_lst)[-1]
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next classSolution: defreorderList(self, head: ListNode) -> None: """ Do not return anything, modify head in-place instead. """ slow, fast = head, head while fast and fast.next: slow = slow.next fast = fast.next.next
classSolution: defrotateRight(self, head: ListNode, k: int) -> ListNode: if k == 0ornot head ornot head.next: return head n = 1 cur = head while cur.next: cur = cur.next n += 1 if (add := n - k % n) == n: return head cur.next = head while add: cur = cur.next add -= 1 ret = cur.next cur.next = None return ret
break curr_node = curr_node.next i += 1 out = new_head.next return out
160 Easy
1 2 3 4 5 6 7
classSolution: defgetIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: A, B = headA, headB while A != B: A = A.nextif A else headB B = B.nextif B else headA return A