How do you serialize and deserialize a binary tree?
Simple meaning
Preorder with explicit null markers (or BFS with nulls) encodes structure uniquely
Open the full page for Why, Steps, Example and Key takeaway.
Panel-ready DSA interview set questions for freshers and experienced developers. Practice at Coding Cadre in Faridabad, or Online from Delhi NCR.
Preorder with explicit null markers (or BFS with nulls) encodes structure uniquely
Open the full page for Why, Steps, Example and Key takeaway.
DFS returns the best downward gain through one child (or zero if negative).
Open the full page for Why, Steps, Example and Key takeaway.
The first preorder value is the root
Open the full page for Why, Steps, Example and Key takeaway.
BFS with (node, column), incrementing column on right and decrementing on left, collecting into a map from column to list.
Open the full page for Why, Steps, Example and Key takeaway.
Reverse postorder: flatten right, flatten left, then splice the old right onto the tail of the left and null the left.
Open the full page for Why, Steps, Example and Key takeaway.
Inorder should be sorted
Open the full page for Why, Steps, Example and Key takeaway.
Push the left spine onto a stack.
Open the full page for Why, Steps, Example and Key takeaway.
Postorder return (is_bst, min, max, size) for each subtree.
Open the full page for Why, Steps, Example and Key takeaway.
Keep a max-heap of the lower half and a min-heap of the upper half, rebalancing so sizes differ by at most one.
Open the full page for Why, Steps, Example and Key takeaway.
Min-heap of the current head of each array with (value, array_id, index).
Open the full page for Why, Steps, Example and Key takeaway.
Greedy with a max-heap of frequencies and a cooldown queue of length k-1.
Open the full page for Why, Steps, Example and Key takeaway.
BFS on the word graph where edges are one-letter differences
Open the full page for Why, Steps, Example and Key takeaway.
Compare adjacent words to extract letter-precedence edges, then topological sort.
Open the full page for Why, Steps, Example and Key takeaway.
A min-heap of (distance, node) always settles the closest unsettled vertex and relaxes its edges.
Open the full page for Why, Steps, Example and Key takeaway.
Kahn: queue all zero-indegree courses, append them to the order, and decrement neighbors.
Open the full page for Why, Steps, Example and Key takeaway.
Tarjan DFS with discovery time and low-link: an edge u-v is a bridge if v cannot reach an ancestor of u, i.e., low[v] > disc[u].
Open the full page for Why, Steps, Example and Key takeaway.
Kruskal sorts edges by weight and adds an edge if Union-Find says the ends are in different components: O(E log E).
Open the full page for Why, Steps, Example and Key takeaway.
Bellman-Ford style relaxation for k+1 rounds, or BFS/Dijkstra on a state (city, stops).
Open the full page for Why, Steps, Example and Key takeaway.
dp[i][j] is the min of replace, delete, or insert to turn word1[:i] into word2[:j], with a free diagonal copy when characters match.
Open the full page for Why, Steps, Example and Key takeaway.
dp[i][j] = dp[i-1][j-1]+1 on a match, else max of skip either character.
Open the full page for Why, Steps, Example and Key takeaway.