Sheet1
Theme Name Level
Heap Profit Maximisation Easy
Merge sort Merge K Sorted Lists Hard
Heap Magician and Chocolates Medium
Set Distinct Numbers in Window Medium
Tree Valid BST from Preorder Medium
Tree Kth Smallest Element In Tree Medium
Tree Merge two Binary Tree Easy
Tree Path to Given Node Easy
Tree Invert the Binary Tree Easy
Tree Diagonal Traversal Medium
Tree Reverse Level Order Medium
Tree Populate Next Right Pointers Tree Hard
Tree Root to Leaf Paths With Sum Medium
Tree Min Depth of Binary Tree Medium
Tree Flatten Binary Tree to Linked List Hard
Graph Path in Directed Graph Easy
Page 1
Sheet1
Comments
I built a max heap and insert all the array elements in it. Then, I summed (B times) the first element, that is, A[0]. But, after sum
The challenge was the merge function. Basically, I check if the value from the left list is smaller than the value of the right list.
Similar with “Profit Maximisation”. Push all elements from the vector into the priority queue. Iterate A times, removing the top,
I created a set of Pair <value, frequency> with the values of each sliding window adding its size to the vector the will return the
I created a function to insert an value into a binary search tree. Then I inserted all the elements of vector A. After that, I dfs in
I dfsed in order pushing the value into the vector. Then I returned the (B-1)th value of the vector. Another idea is dfs, decreme
I used the first node from the parameters as the node to return. I call the function for both roots as current nodes. If each node
I created a hash map to save the parent of some node. Root’s parent equals -1. Then I visit each node and set their children p
The invert function needs to check if both children are null to stop. After this verification, swap both children (temp = left, left =
Basically a pre-order dfs. The rule for the diagonal is: if it goes to the left, increment diagonal, otherwise (that is, if it goes to th
Basically the same idea of the problem called “Diagonal Traversal”. Instead of using “diagonal” (as in that problem), I used a v
Basically the same idea of the problem called “Reverse Level Order”. Instead of using value, I needed to keep the pointer to th
I keep a stack to push the values that are candidates to be part of the answer. Each call to dfs, I subtract the sum with the nod
Keep a global integer for the minimum. Traverse the tree in pre-order, checking if the current node is a leaf and update minim
I created a vector of TreeNode*, then I traverse the tree in pre-order pushing the pointers into the vector. Finally I set left to nu
I created a vector of vector of integers in order to know the neighbors in a efficient way. Then I bfsed from 1 until I got A or rea
Page 2
Sheet1
ement, that is, A[0]. But, after summing it, I decremented and heapify it.
er than the value of the right list. If so, I add left to the answer and “increment it”, that is, left = left→next. Otherwise, I do the same operation
erate A times, removing the top, incrementing it to the answer (with modulo), and re-add the value/2 (integer division).
ze to the vector the will return the answer. For each sliding window, I add the current value and remove the (i-B)th value.
ts of vector A. After that, I dfs in pre order and check if this read matches the input vector A.
or. Another idea is dfs, decrementing B in order. If B == 0, that’s the answer, that is, the value of the current node.
s as current nodes. If each node exists, so I sum their values and set it to the first node (that’s the one I’m going to return). If only this first n
ach node and set their children parent (in the hash map) to the current node’s value, that is, if left (or right) child is not null, parent[left] (or p
both children (temp = left, left = right, right = temp). Obs.: at this point, I don’t need to worry about NULL child. After that, I call the invert fun
otherwise (that is, if it goes to the right) you keep the diagonal value. I create a struct Node in order to help me with the order. The struct ha
” (as in that problem), I used a variable called level.
needed to keep the pointer to the TreeNode. Then I sorted the vector of Node (the struct Node I created). Finally, I took each group of Nod
s, I subtract the sum with the node’s value. When I get 0 to this sum variable, I check if the node is a leaf. If so, I add the stack values to the
node is a leaf and update minimum variable accordingly.
the vector. Finally I set left to null of each element, and right to the next node of the vector.
I bfsed from 1 until I got A or reach the end of code, returning 0.
Page 3
Sheet1
xt. Otherwise, I do the same operation with the right list. Another algorithm is read each node and add it into a vector, sort this vector and ad
nteger division).
e the (i-B)th value.
current node.
e I’m going to return). If only this first node is null, so I assign the second node to it. If only the second node is null, I don’t need to do anythi
right) child is not null, parent[left] (or parent[right]) equals current node’s value.
ULL child. After that, I call the invert function for each not null child.
o help me with the order. The struct has three integers: one for the order (each element I add, I increment this order variable), one for the va
ated). Finally, I took each group of Nodes (grouped by level) and apply the rule (next of one node points to the right node at the same level)
eaf. If so, I add the stack values to the answer.
Page 4
Sheet1
it into a vector, sort this vector and add each element to a brand new linked list.
node is null, I don’t need to do anything.
ment this order variable), one for the value and one for the diagonal. I visit each node adding this Node with the order++, diagonal and value
ts to the right node at the same level).
Page 5
Sheet1
e with the order++, diagonal and value. After the dfs, I sort this vector of nodes and add its values to a vector of integer.
Page 6
Sheet1
vector of integer.
Page 7