Skip to content

Instantly share code, notes, and snippets.

View braddotcoffee's full-sized avatar

Brad // Cloudzy braddotcoffee

View GitHub Profile
@braddotcoffee
braddotcoffee / solution.py
Created February 28, 2024 15:59
513. Find Bottom Left Tree Value
# 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
class Solution:
def _left_first_traverse(self, node, depth):
if node is None:
return
@braddotcoffee
braddotcoffee / solution.py
Created December 13, 2023 22:45
322. Coin Change
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
min_coins = [float("inf")] * (amount + 1)
min_coins[0] = 0
for i in range(1, amount + 1):
for coin_val in coins:
if i - coin_val < 0:
continue
min_coins[i] = min(
@braddotcoffee
braddotcoffee / solution.py
Created November 21, 2023 22:02
78. Subsets
class Solution:
def depth_first_search(self, nums: List[int], curr_state: List[int], curr_idx: int):
if curr_idx == len(nums):
self.final_result.append(curr_state.copy())
return
num = nums[curr_idx]
curr_state.append(num)
self.depth_first_search(nums, curr_state, curr_idx + 1)
curr_state.pop()
@braddotcoffee
braddotcoffee / solution.py
Created November 19, 2023 16:00
199. Binary Tree Right Side View
from collections import deque
# 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
class Solution:
def append_or_overwrite(self, node: TreeNode, depth: int, seen: List[int]):
if len(seen) == depth:
@braddotcoffee
braddotcoffee / solution.py
Created November 17, 2023 02:45
102. Binary Tree Level Order Traversal
# 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
class Solution:
def traverse_tree(self, node: Optional[TreeNode], depth: int, levels: list[list[int]]):
if node is None:
return
@braddotcoffee
braddotcoffee / solution.py
Created November 17, 2023 02:21
235. Lowest Common Ancestor of a Binary Search Tree
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
"""
We need to binary search until we find our node p and our node q
- generate a set of nodes we've seen while looking for p
@braddotcoffee
braddotcoffee / solution.py
Created November 11, 2023 05:39
572. Subtree of Another Tree
# 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
class Solution:
def identical_tree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]):
if root is None or subRoot is None:
@braddotcoffee
braddotcoffee / solution.py
Last active November 11, 2023 05:19
100. Same Tree
# 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
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if p is None or q is None:
return p == q
@braddotcoffee
braddotcoffee / solution.py
Created November 10, 2023 16:58
110. Balanced Binary Tree
# 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
"""
We need to calculate the depth of the left and right
subtree all the way down. If at any point that
depth differs by > 1, we need a way to easily return
@braddotcoffee
braddotcoffee / 1-pull_request_template.md
Last active November 28, 2023 02:22
Example PR Template

Motivation

Why is this change necessary? What problem does it solve?

Closes #ISSUE_NUMBER

Implementation

How does this PR solve the problem? What technical approach is taken?

Testing

How did you verify that this works? Were automated tests written?