This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Leetcode : 449 | |
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Codec: | |
def serialize(self, root: Optional[TreeNode]) -> str: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List, Optional | |
# class TreeNode: | |
# def __init__(self, val=0, left=None, right=None): | |
# self.val = val | |
# self.left = left | |
# self.right = right | |
def inorder_traversal(root: Optional['TreeNode']) -> List[int]: | |
# holds the inorder result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Leetcode 210: Kahn's Algorithm | |
# https://www.youtube.com/watch?v=cIBFEhD77b4 | |
from collections import defaultdict, deque | |
from typing import List, Tuple | |
def kahn_toposort(n: int, edges: List[Tuple[int, int]]) -> List[int]: | |
# create adjacency graph as a dictionary where each node maps to its neighbors | |
graph = defaultdict(list) | |
# indegree list using list comprehension, initially all 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Leetcode 48 : Rotate Image | |
class Solution: | |
def rotate90(self, matrix: List[List[int]], offset: Tuple[int, int], n: int): | |
if n > 1: | |
# get offset | |
x, y = offset | |
# rotate edges : top << left << bottom << right | |
top_i, top_j = x, y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Leetcode: 114. Flatten Binary Tree to Linked List | |
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Leetcode 25 | |
# Definition for singly-linked list. | |
# class ListNode: | |
# def __init__(self, val=0, next=None): | |
# self.val = val | |
# self.next = next | |
class Solution: | |
def reverseList(self, head: ListNode): | |
prev = None | |
curr = head |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Leetcode 61: Reorder List | |
# function to find mid of a list | |
def findMidofList(self, head: ListNode) -> ListNode: | |
slow, fast = head, head | |
while fast and fast.next: | |
slow = slow.next | |
fast = fast.next.next | |
return slow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for a binary tree node. | |
# class TreeNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Codec: | |
def serialize(self, root): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to find prime factors of a number | |
def primeFactors(num): | |
# Function to reduce num by a factor | |
def reduceByFactor(factor): | |
nonlocal num | |
while num % factor == 0: | |
num = num // factor | |
return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 deleteAndReplaceWithMin(self, root: TreeNode): | |
NewerOlder