Problems
September 4, 2022
Construct binary tree from inorder and postorder traversal
We know, the last index of postorder traversal is always the root of the tree, from that info, we can find the root in inorder traversal too. From there, we will find the left inorder subtree and...
ReadSeptember 4, 2022
Binary tree level order traversal II
This is a classing BFS problem. We should traverse the whole tree with BFS, and store the values level by level to a list. Then combine each level to a list and return the reversed list as result....
ReadSeptember 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
Binary tree zigzag level order traversal
We will traverse the tree with BFS and append the values of each level to our result list. And for each alternative level we change the order. Finally return the result after we visit each node....
ReadSeptember 4, 2022
Unique binary search trees
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, and...
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 4, 2022
delete-node-in-a-bst
We will check whether the key is greater than root, then the key is in right subtree, if the key is less than root, then key is in left subtree. If the key is the root, then we check, if there is a...
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