Roadmap › Stage 6: Trees › Pattern 27 of 53
DSA pattern guide
Tree — DFS
Depth, paths, diameter, balanced or symmetric checks.
Use it when
Depth, paths, diameter, balanced or symmetric checks.
Key idea
Recurse to the children and return what the parent needs (height, sum). Update a global answer on the way back.
Practice problems, in order
- Inorder TraversalEasy
- Symmetric TreeEasy
- Diameter of Binary TreeEasy
- Balanced Binary TreeEasy
- Maximum Depth of Binary TreeEasy
- Path SumEasy
- Subtree of Another TreeEasy
- Path Sum IIMedium
- Path Sum IIIMedium
- Count Complete Tree NodesMedium
- All Nodes Distance K in Binary TreeMedium
- Binary Tree Maximum Path SumHard
- Binary Tree CamerasHard
How to practise this pattern
- Read the problem and look for the signal above before writing any code.
- Say the brute force out loud, with its time complexity, then ask what work it repeats.
- Apply the key idea and write the optimised version yourself.
- If you're stuck for more than 10 minutes, take one small hint, not the full solution.
- Move to the next problem only when you can explain why this pattern fits.
Stuck on one of these?
The AlgoSolveo extension (free, launching soon) sits beside the LeetCode problem and gives the smallest hint that gets you moving, never the full answer. Solved problems tick off on your roadmap.
Join the extension waitlistTrack it on the roadmap