Skip to content

Instantly share code, notes, and snippets.

View phongngtuan's full-sized avatar

Phong Nguyen phongngtuan

View GitHub Profile
@phongngtuan
phongngtuan / binary-tree-pruning.py
Created September 16, 2019 15:23
binary-tree-pruning.py
class Solution:
def pruneTree(self, root: TreeNode) -> TreeNode:
if root == None:
return None
left = self.pruneTree(root.left)
right = self.pruneTree(root.right)
if root.val == 1 or left or right: # not prunable
root = TreeNode(root.val)
root.left = left
root.right = right
import heapq
class Solution:
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
heap = []
for cost in costs:
heapq.heappush(heap, (-abs(cost[0] - cost[1]), cost[0], cost[1]))
N = len(costs) / 2
total = 0
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
if n == 1:
return [[1]]
mat = [[0 for i in range(n)] for j in range(n)]
counter = 1
for i in range(0, n):
x, y = i, i
width = n - i * 2
height = width - 2
class TrieNode:
def __init__(self, c: str, path: str):
self.c = c # type: str
self.counter = 0 # type: str
self.path = path # type: str
self.children = dict() # type: Dict[str, TrieNode]
def add(self, key: str, path: str):
if len(key) == 0:
self.counter += 1