A Stack is an Array, but where you only add and remove the very last element.
All operations on a stack take O(1) time. This is because you never insert or delete an element in the middle of the array, so you never have to shift any elements over.
Here's an example:
Read/Write Time O(1)
Append/Pop Time O(1)
You'll get practice using Stacks in the next group of problems. Usually you don't have to do much thinking - Stacks just arise naturally when you solve the problem.
TreeNode.of
function can be used to create an entire binary tree using 1 line of code. The input is a BFS traversal of the tree including nulls, and the output is the root node of the tree.TreeNode.of([1, None, 2, None, 3])
ListNode.of
function lets you create an entire Linked List using 1 line of code. The input is an array, and the output is the head of the Linked List.ListNode.of([1, 2, 3])
ListNode.of([1, 2, 3], 1)
GraphNode.of
function lets you create an entire Graph using 1 line of code. The input is an Adjacency Matrix, and the output is the first node in the matrix.GraphNode.of([ ['A', 1, 2], ['B', 2], ['C', 0] ])
[['__init__', 15], ['add', 16], ['get']]
c = MyClass(15) c.add(16) c.get()