Queue and Stack

Overview

Queue and stack are just like a store. You just want to put and pick the items one by one. The time complexity of put and pick is O(1)

Python makes queue and stack easier because we can use list to finish the functions of queue and stack.

1
2
3
4
5
6
7
8
9
my_queue = []
my_queue.append(4)
my_queue.append(5)
print(my_queue.pop(0)) # we get 4

my_stack = []
my_stack.append(4)
my_stack.append(5)
print(my_stack.pop()) # we get 5

Samples

Travel the tree

No.94 - Binary Tree Inorder Traversal

No.103 - Binary Tree Zigzag Level Order Traversal

Pair Match

No.20 - Valid Parentheses

Store the data in order[Some special questions]

No.42 - Trapping Rain Water

To be continued…