Problems
September 5, 2022
Linked list cycle II
We can use Floyd's cycle detection algorithm to find the position. We will create a fast and slow pointer, fast pointer is twice as fast as the slow pointer. If the two pointer doesn't meet, that...
ReadSeptember 5, 2022
Battleships in a board
We will run DFS to explore all the values after we found one X
and mark that as visited and count the number of battleship by 1. After traverse the whole board, we will return the count...
September 4, 2022
Evaluate division
We will first create an adjacency list from the provided equations and values list. Each node will have the related node and thier weight in it. Then we run DFS to find the result. Finally, we will...
ReadSeptember 4, 2022
Unique binary search trees II
We will take the top-down approach to solve the problem. We will start from 1 and for every position, we take is as root, and build a tree with the left and right subarray for that root position,...
ReadSeptember 4, 2022
Amount of time for binary tree to be infected
We will first create an adjacency list for our tree. Then we run a simple BFS to traverse all the nodes, and the number of level we need to traverse is our time.
Time Complexity: O(n)
...
September 4, 2022
Verify preorder serialization of a binary tree
We will split the string with ,
as delimeter, and pop the last value from it to check whether it's a #
or not, if not, we immediately return false. Then we iterate over the...
September 4, 2022
Count complete tree nodes
We will count the height of the left and right subtree. If they are equal, then the it's a complete tree, so the number of node will be 2^n-1
. If it's not a complete tree, then the...
September 4, 2022
Populating next right pointers in each node II
We will traverse the tree with BFS and in each level, we will append the nodes to a list. After each level traversal, we take these nodes, and assing the next node to it's right node. We will repeat...
ReadSeptember 4, 2022
Binary search tree iterator
We will create a queue to store our values while create the oterator class. Then for next, we will pop the values from the queue and return, for hasNext, we will check it the queue is empty or not....
ReadSeptember 4, 2022
Recover binary search tree
We will traverse the tree with inorder traversal and add each nodes to the a list. Then we sort the values of each nodes, then iterate over the sorted values, and assign the values to respective tree...
Read