Skip to content

Instantly share code, notes, and snippets.

View ssshukla26's full-sized avatar
💭
Learn Apply Repeat

Sunny Shukla ssshukla26

💭
Learn Apply Repeat
View GitHub Profile
@ssshukla26
ssshukla26 / SerializeDeserializeBT.py
Created September 18, 2025 07:35
Serialize-Deserialize a BT / BST
# 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:
@ssshukla26
ssshukla26 / IterativeInorderBSTTraversal.py
Created September 7, 2025 09:46
Iterative Inorder Traversal of BST
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
@ssshukla26
ssshukla26 / KahnsAlgorithmForTopSort.py
Created September 7, 2025 02:17
Kahn's Algorithm
# 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
@ssshukla26
ssshukla26 / RotateMatrixBy90.py
Created August 18, 2025 04:25
Rotate A Matrix
# 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
@ssshukla26
ssshukla26 / FlattenBinaryTree.py
Created August 14, 2025 07:01
Flatten Binary Tree
# 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:
@ssshukla26
ssshukla26 / ReverseNodesInKGroups.py
Created July 27, 2025 22:02
Reverse Nodes in k-Groups in a List
# 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
@ssshukla26
ssshukla26 / ReorderList.py
Created July 27, 2025 22:00
Reorder List
# 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
@ssshukla26
ssshukla26 / BTEncDec.py
Created November 27, 2021 03:25
Serialize and Deserialize Binary Tree [LeetCode 297]
# 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):
@ssshukla26
ssshukla26 / PrimeFactors.py
Last active November 24, 2021 06:47
Get all prime factors of a number
# 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
@ssshukla26
ssshukla26 / DeleteNodeBST.py
Created November 22, 2021 18:03
Delete Node from a BST [LeetCode 450]
# 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):