Problems
September 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
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
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
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
Convert sorted list to binary search tree
We will first loop over the entire linked list and add the values to a list. Now we will take the middle index of the list as our root and recursively build both the left and right subtree and return...
ReadSeptember 4, 2022
Populating next right pointers in each node
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
Vertical order traversal of a binary tree
We will first traverse the tree with DFS and store it's values along with row and column in the the process. Then we sort them by column value and group them by column value. Finally, take all these...
ReadSeptember 3, 2022
Balance a binary search tree
We will first traverse the tree with inorder traversal, and append all the values to an array. Then we create a binary search tree with the values, where root will be the middle of the array, thus we...
ReadSeptember 3, 2022
Numbers with same consecutive differences
We will start from 1, and then take the last digit of the number, add k to it and append to the right of the current number, until we reach the required length of n. We append every number on the way...
Read