Tree Traversals DFS
94. Binary Tree Inorder Traversal
a. Recursive solution
A Heap
is a special Tree-based data structure in which the tree is a complete binary tree. Generally, there are two types of Heap
: Max-Heap (root node is greater than its child nodes) and Min-Heap (root node is smaller than its child nodes).
We can simply iterate over all items from the given string and compare the adjacent values each time with the help of stack before pushing the element in.
We can’t use the similar solution we did for Design a Queue with Stack. It is because unlike Stack
, moving elements from one Queue
to another one won’t change the sequence of elements. We have to pop out all previous elements added into the queue when adding a new element, in this way we can simulate a Stack.
Since Queue
is FIFO
but Stack
is FILO
. If we need to use Stack
to implement a Queue
, we need to use at least two Stack
s. So we use one stack which only handle Push
operations, and another Stack
which only handle Pop
/Peek
operations. And we move elements from the Pop
only Stack
to the other one when Pop
/Peek
get called. It will reverse the FILO
stack elements sequence after that. So we get a FIFO
sequence.
Search if a given string pattern (needle) is part of a target string (haystack) is a common problem. The naive approach is to use two nested loops with O(n * m) time complexity. KMP is a better way which has a better performance.
Let’s take a look at a easy problem on Leetcode 27. Remove Element. We will demonstrate how to remove an element from an array without allocating extra space for another array.
Usually when you get a problem about searching the common items between multiple strings, the brute-force solution’s time complexity is usually too high. We can use hashmap to lower the time complexity.
For a given linked list, it has 3 common methods: GetByIndex, AddTo(Head/Tail/ToIndex), Delele. Similar to SQL’s CURD. Let’s see how to design a linked list class. 707. Design Linked List
Let’s take a look at a easy problem on Leetcode 203. Remove Linked List Elements. We will demonstrate how to remove elements from a linked list.