What is a tree?
A hierarchical data structure made of nodes, with one root and child nodes.
What does each node have?
a value and references to children
What is a binary tree?
A tree where each node has at most two children: left and right.
What is a root?
top node
What is a leaf?
node with no children
What is depth/height?
distance from root
What is a subtree?
a node + its descendants
What is DFS?
depth first search - go as deep as possible before backing up
What is DFS used for?
searching, counting nodes, checking properties recursively
What is preorder?
node -> left -> right
What is inorder?
left -> node -> right
What is postorder?
left -> right -> node
What is the usual DFS skeleton?
def dfs(node):
if not node:
return
dfs(node.left) dfs(node.right)
What is BFS?
breadth first search - visit nodes level by level
What is BFS used for?
shortest path, level order traversal, nearest/minimum depth problems
What is the skeleton for BFS?
from collections import deque
queue = deque([root])
while queue:
node = queue.popleft()
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)How do you explain the difference between DFS vs BFS?
DFS goes deep first using recursion or a stack, while BFS explores level by level using a queue
What is a heap?
a special data structure where you can quickly get the min or max element
What is a min heap?
smallest value on top
What is a max heap?
largest value on top
When are heaps used?
you need the top K elements, you need fast access to min/max repeatedly
What is the syntax for heap in python?
import heapq
heap = []
heapq.heappush(heap, 5)
heapq.heappush(heap, 2)
heapq.heappush(heap, 8)
smallest = heapq.heappop(heap)
What is the time complexity for heaps?
O(log n) insert and remove
What are arrays usually used for?
scan once, track min/max, sliding window, two pointers