Problems


July 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...

Read

July 20, 2022

N-ary tree preorder traversal

We will traverse the tree with recursive DFS, as it is preorder traversal, we first append the root to our result and then traverse all the children. Time Complexity: O(n) Space...

Read

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:...

Read

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:...

Read

July 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

July 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...

Read

July 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...

Read

July 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...

Read

July 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...

Read

July 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...

Read
... 88 89 90 91 92 ...