trees
trees
graph sheet
https://docs.google.com/spreadsheets/d/1GsGN5VbQcEEpDGW1SYgGKgpqV1tbLLwMz4cMo-
cpMfs/edit#gid=0
Trees sheet
https://docs.google.com/spreadsheets/d/
1aXh_ZVkI79_orja9RakwmfQMMSLHi3w6veQ2EjREDg8/edit#gid=0
DP sheet
https://docs.google.com/spreadsheets/d/
1vO1RyO0O0lZoSiI8Ib3QrDovCDdGMq_CmJJ_rhDsTp4/edit#gid=0
cses website
codeforces
TREES
dfs
bfs
zig zag traversal
diameter of a binary tree
path sum
maximum path sum
left-view
right-view
symmetric tree
print nodes in top view(gfg)
bottom view
vertical order traversal(****)
boundary traversal of a binary tree(very good ques)
all nodes at a distance k in a binary tree(***)
burn a binary tree
sum root to leaf numbers
lowest common ancestor(interview common question)
smallest subtree with all the deepest nodes
maximum width of a binary tree
second minimum node in a binary tree
Path Sum III
Path Sum II
Flatten Binary Tree to Linked List
Populating Next Right Pointers in Each Node
Sum of Left Leaves
Binary Tree Pruning
search in bst
insert in bst
validate the bst
Insert into a Binary Search Tree
Construct Binary Tree from Inorder and Preorder Traversal{while doing this using
PreIndex,map should be passed by Reference}
Construct Binary Tree from Inorder and Postorder Traversal{while doing this using
PostIndex,map should be passed by Reference}
kth smallest element in a BST
minimum distance between BST Nodes
Construct Binary Search Tree from Preorder Traversal
Convert Sorted List to Binary Search Tree(***)
lowest common ancestor of BST
Maximum Sum BST in Binary Tree(********)
AVL Trees(ek baar dekh lena)
GREEDY
HEAP
DP
unique paths -- travserse from bottom, go top or left and add top+left with i,j
reaching 1 return 1 i,j<0 return 9
unique paths II -- same as unique but to handle obsacle (i>=0 && j>=0 && v[i]
[j]==1) this is obstacle, return 0
Minimum Path Sum -- same as unique path, top,left get min(top,left)+v[i][j]
triangle(****) -- since its not sq two index Rin,in , bottom, bottomRight-
(Rin+1,in),(Rin+1,in+1) get min_v[Rin][in]
minimum falling path sum(**)--apply for loop, since we are supposed to check all
possibilites in row, inside for loop apply recursion (topLeft,top,topRight) get min
Subset Sum Problem -- start from end if sum==0&&n==0 true, else false, now
pick(sum-arr[n-1],n-1) only if sum>=arr[n-1] else notPick(sum,n-1), return pick or
notPick
0-1 knapsack -- same as subset problem excepet values are given here
coin change II(solve this -- recursion) -- here same as subset but since mutiple
picking of coins is allowed we remain pick at (amount-coin[n-1],n) return
pick+notPick
write in notes and do dry run
coin change -- same as coin change II we just want number of possibilties everytime
we pick just add 1 to it
minimum sum partition -- first find totalsum of arr, at index==n return (abs(sum-
(Totalsum-sum))), everytime we pick add arr[index] to our sum
Longest Palindromic Substring -- (diff type of dp, do it tabulation dont try memo)
--- cant solve by reversing and lcs =, to solve this use all 1 length are
palindrome then check from length k=2ton then inside i=0ton-k(no.of strings with
length k) check if
s[i][j]==1 && dp[i+1][j-1]==1||k==2 thats a valid k length palindrome store length
and start index, here end index j=i+k-1, ans then lastly print use seaparet loop
Palindromic Substrings -- (same as above) just want total no. of palindrome if the
condition satisfies simply res++, initially res=n as 1 length are palindrome
Best Time to Buy and Sell Stock II(****) - if buy==1 we got pick,notPick if we pick
that mean reduce from amount, -v[i]+Solve(i+1,1-buy), when sold pick=v[i]
+Solve(i+1,1-buy) get max
Best Time to Buy and Sell Stock with Transaction Fee -- since fee is applied after
selling we got to reduce after sold in sold cond v[i]+Solve(i+1,1-buy)-fee , return
max
Best Time to Buy and Sell Stock III -- since 2 transactions everytime we sell -v[i]
+Solve(i+1,count-1,1-buy) and base cond if count reaches 0 return 0;
Best Time to Buy and Sell Stock IV -- since k transactions everytime we sell -v[i]
+Solve(i+1,k-1,1-buy) and base cond if k reaches 0 return 0;
Best Time to Buy and Sell Stock with Cooldown Period -- after sold we have to
cooldown for a day so -v[i]+Solve(i+2,1-buy) with i+2 it exceeds n so base
condition index>=n
GRAPH
remember to use extra loop if the graph goes like connected components
dfs on graph --keep track of visited array and if node is not visited then we call
for dfs for the node untill all visited array ele go true
bfs on graph -- we use queue intially push 0 then using while(!q.empty()) we keep
checking visited array if not visited mark visited then push to queue
Detect cycle in an undirected graph-- same as dfs with extra condition else if (x!
=parent)
Is Graph Bipartite? -- same dfs/bfs with extra condition we alternatively assign
value 1/0 to the nodes and if already visited node get same color as earlier then
not a bipartitie
Detect cycle in an directed graph --visited,dfsvisited when already visited node
goes 1 in dfsVisited then there's a cycle, and everytime we backtrack
dfsVisited[node]=0
grid problems----
Number of Provinces -- for the extra loop we use just increment a variable there
ans++;
Number of Islands -- assuming the grid is surrounding the water,solve dfs in all
four direction with base condition isSafe function and increment ans in extra for
loop we use
Surrounded regions -- here we cant assume to be surrounded in water instead we
traverse in all edges to find O and mark them 1 with their consecutive O's then at
last traversing the
whole gird mark 1's as O ans O's as X, done
Rotting Oranges(***) -- find all rotten oranges push them to queuu<pair> with (i,j)
use while loop inside run a loop c=q.size times
to traverse queue and tranverse a node in all four directions to find new row and
column
check for isSafe func and if safe mark grid[i][j] as 2 ans push to queue
evnentually the no of times while loop runs is the ans
Number of Distinct Islands(not done in class)
Topological Sort -- using stack-dfs -- simply traverse dfs if not visited and keep
push to stack after all calls then separately pop from the stack to print
using Kahn's ALgorithm -- find indegree, push indegree=0 nodes in queue and
traverse through the node and decrement the indgree[node]-- if it goes to 0 push to
queue untill empty then
return res vector where we pushed our node
Course Schedule -- same as kahn's algo just change in making adj list
Course Schedule II -- same as kahn's algo just change in making adj list
Bellman-Ford Algorithm-- Negative weight cycle--run n-1 times then we get accurate
result in negative weights too
Minimum Spanning Tree-- can be solved using Prim's and Kruskal's algo
Maximum Total Importance of Roads-- easier will get tricked thinking of graph
approach instead use simple maths, find degree of each node both ways and the
importance value to it would be
currVal*degree[i] hence use this to get the ans
Minimum Number of Vertices to Reach All Nodes -- this ones simple too just mark
visited to the x[1] in given array and using another loop traverse visited, the not
visited ones push to the
res array, simply indegree