Field note / fundamentals

publishedupdated 2026-05-04#algorithms#trees#traversal#bfs#dfs

Coding Practice: Trees

Ten medium-level tree questions covering traversal, view building, hierarchy assembly, validation, and path-based reasoning.

TL;DR

  • Trees show up in UI hierarchies, menus, comments, org charts, and document structure.
  • I practice both DFS and BFS so I can switch based on the kind of answer I need.
  • The main signals are hierarchy, parent/child relationships, and path accumulation.

Context

Frontend developers work with trees constantly, even if they do not call them trees: React component structure, navigation trees, JSON schemas, nested comments, folder explorers, and rich text nodes.

The Approach

Practice these 10 medium-level questions:

  1. Maximum Depth of Binary Tree — DFS depth baseline.
  2. Binary Tree Level Order Traversal — BFS queue pattern.
  3. Validate Binary Search Tree — boundary propagation.
  4. Lowest Common Ancestor — recurse or parent mapping.
  5. Right Side View — level-based visibility.
  6. Path Sum II — accumulate path state and clone at leaves.
  7. Serialize and Deserialize Tree — stable shape encoding.
  8. Build Tree From Flat Menu Items — parent/child linking by ID.
  9. Compare Two Trees for Structural Equality — recursive mirror check.
  10. Flatten Tree to Linked List Order — preorder restructuring.

Try it in the browser

Browser Practice

Tree practice: level-order traversal

Implement solve(root) and return values level by level. Each node has the shape { value, left, right }.

Sample Tests
1 checks
Function name: solve
Best for JavaScript / TypeScript / JSON practice snippets.Monospace editorRuns in browser sandbox

Trade-offs

  • DFS tends to be simpler to write recursively, but BFS can be cleaner when the problem is level-oriented.
  • Tree building from flat data is practical, but it depends heavily on the quality of the input constraints.

References