Algorithms and Data Structures
- Tree: DFS with recursion DFS with a stack. BFS with a queue.
Level order with DFS: you pass the level and then have a list. you can have a function inside a function to store the level values.
Level order with BFS: you take the length of the queue before you start a level. and everyhting in that lenght will be that level. BFS is more intutive to level order.
-
stack. you initialize a list normally.
-
list.append() -> to append to the top
-
list.pop() -> you pop the latest element.
-
queue. use a deque from collections. from collections import deque d.append() -> append normally d.popleft() -> you get the first element.
you can do a normal list and do pop(0) but this would be very ineffecient - O(n) each time.