Problems
July 20, 2022
Binary tree postorder traversal
We will traverse the tree with recursive DFS, as it is postorder traversal, we first traverse the left subtree, the the right subtree and then append the root to our result. Time Complexity:...
ReadJuly 20, 2022
Kth largest element in a stream
We will create a min heap from the elements of the stream. We will make sure heap size is always k. That means the smallest element will ve at the heap's top and the largest element will be at the...
ReadJuly 20, 2022
N-ary tree postorder traversal
We will traverse the tree with recursive DFS, as it is postorder traversal, we first traverse all the children and then append the root to our result.
Time Complexity: O(n)
Space...
July 20, 2022
Binary tree inorder traversal
We will traverse the tree with recursive DFS, as it is inorder traversal, we first traverse the left subtree then append the root, and lastly we traverse the right subtree. Time Complexity:...
ReadJuly 19, 2022
Linked list cycle
We will take two pointer, fast and slow. Fast goes twice as fast as slow. Then we run both pointers at the same time, if there is a cycle, then this two pointers will meet, else we will goes to the...
ReadJuly 19, 2022
Min stack
We we have 2 stack. One will be the regular and the another will be min stack. When we push something in stack, we push the value in regular stack and we check the top element of the min stack. If...
ReadJuly 19, 2022
Evaluate reverse polish notation
We can go through each character and put this on a stack. When we find any arithmetic symbol, we pop last 2 element for the stack, do the operation and push back the value to the stack. After the...
ReadJuly 19, 2022
Path sum
We can run DFS from root to leaf and calculate whether the target is equal to the sum of all nodes. We can do it recursively. Our base case will be if the node is empty, then we will return false and...
ReadJuly 19, 2022
Binary search
The problem itself is called binary search, all we need to do a classic binary search. We will choose a mid, if the target value is greater than the mid value, we move our left pointer to mid or if...
ReadJuly 19, 2022
Pascals triangle
We know n level pascals triangle will have n level. We will start with level 1, we put 1 there. Then we will start a loop from level 2 to level n. Then we fill up the whole level with 1. Then we loop...
Read