DSA & Implementation · core
Trees
Binary trees, BSTs, DFS/BFS.
Mental model
A tree is recursion made concrete: almost every tree problem is "solve it for the children, then combine". DFS (pre/in/post-order) is a recursion or an explicit stack; BFS is a queue, level by level. Pick the traversal that matches what you need to combine.
How to study Trees
Begin by restating the mental model in your own words, then connect it to a concrete system you have built or operated. Name the mechanism, the constraint it addresses, and the trade-off it introduces. Use VisuAlgo — Binary Search Tree to check details, but close the source before writing your explanation. Retrieval is the learning step; rereading is only preparation.
Next, compare Trees with Graphs. Ask what changes in correctness, latency, resource use, operability, and failure recovery. Complete Binary tree level-order traversal, Invert Binary Tree, Maximum Depth of Binary Tree and preserve the command, input, output, and one failed attempt as evidence. Finish by explaining the idea without jargon to someone who has not studied the track.
Proof of understanding
- Explain the mechanism from first principles and identify the state it reads or changes.
- Give one situation where the concept is the right choice and one where it is not.
- Predict a realistic failure mode before running the drill, then compare the prediction with evidence.
- Connect the result to a roadmap or build artifact instead of treating the concept as isolated trivia.
Common mistakes
- Forgetting the null/empty base case
- Using DFS when the problem is about levels or shortest depth (use BFS)
- Returning the wrong thing from recursion — separate "answer" from "info passed up"
Learn from primary sources
Practice and explain it back
Binary tree level-order traversal
Return the values of a binary tree grouped by level, top to bottom.
Expected evidence: levelOrder of [3,9,20,null,null,15,7] -> [[3],[9,20],[15,7]]
Open the interactive drill →Invert Binary Tree
LeetCode #226 — Invert Binary Tree. Solve on LeetCode, then implement here if you want it in your drill queue. https://leetcode.com/problems/invert-binary-tree/
Expected evidence: Pass all LeetCode test cases for this problem.
Open the interactive drill →Maximum Depth of Binary Tree
LeetCode #104 — Maximum Depth of Binary Tree. Solve on LeetCode, then implement here if you want it in your drill queue. https://leetcode.com/problems/maximum-depth-of-binary-tree/
Expected evidence: Pass all LeetCode test cases for this problem.
Open the interactive drill →Lowest Common Ancestor of a Binary Search Tree
LeetCode #235 — Lowest Common Ancestor of a Binary Search Tree. Solve on LeetCode, then implement here if you want it in your drill queue. https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
Expected evidence: Pass all LeetCode test cases for this problem.
Open the interactive drill →Serialize and Deserialize Binary Tree
LeetCode #297 — Serialize and Deserialize Binary Tree. Solve on LeetCode, then implement here if you want it in your drill queue. https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
Expected evidence: Pass all LeetCode test cases for this problem.
Open the interactive drill →Subtree of Another Tree
LeetCode #572 — Subtree of Another Tree. Solve on LeetCode, then implement here if you want it in your drill queue. https://leetcode.com/problems/subtree-of-another-tree/
Expected evidence: Pass all LeetCode test cases for this problem.
Open the interactive drill →Review prompts
- In a recursive tree solution, why separate "the answer" from "the value returned to the parent"?
Build evidence
Use a roadmap capstone to turn this concept into working evidence.