# Tree Problem Patterns

> Solve the harder tree questions: diameter, maximum path sum, views, lowest common ancestor, serialisation and distance problems. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/dsa-placement/tree-patterns
Last updated: 2026-11-28

Solve the harder tree questions: diameter, maximum path sum, views, lowest common ancestor, serialisation and distance problems.

Intermediate · about 8 hours · difficulty 4/5

Learn first: [Binary Tree Traversals](https://www.internhack.in/roadmaps/tracks/dsa-placement/binary-trees)

Once traversals are automatic, the harder tree problems come from a few recurring ideas.

### Key patterns

- **Return one thing, update a global**: compute height but update the answer with left + right at each node (Diameter of Binary Tree, Binary Tree Maximum Path Sum, Balanced Binary Tree).
- **Views by level or column**: right side view via BFS, top and bottom view via BFS with horizontal distance and a map, vertical order traversal.
- **Lowest Common Ancestor**: return the node if it is p or q; if both sides return non-null, the current node is the LCA.
- **Root-to-node paths**: path sums with backtracking, Path Sum II, Path Sum III (prefix sums on a tree).
- **Treat the tree as a graph**: store parent pointers and BFS outward (All Nodes Distance K, Minimum time to burn a tree).
- **Serialisation**: Serialize and Deserialize Binary Tree with preorder and null markers.

### Classic problems

Diameter of Binary Tree, Balanced Binary Tree, Binary Tree Maximum Path Sum, Binary Tree Right Side View, Top View (GfG), Vertical Order Traversal, Lowest Common Ancestor of a Binary Tree, Path Sum III, All Nodes Distance K, Serialize and Deserialize Binary Tree, Maximum Width of Binary Tree.

### Pitfalls

- In Maximum Path Sum, returning left + right + node upward. Only one branch can be extended to the parent.
- Overflow in Maximum Width when indices double each level; normalise indices per level.

You are ready to move on when you can solve Maximum Path Sum and LCA and explain what each recursive call returns.

## Key concepts

- **Height-based global answers**
- **Diameter and maximum path sum**
- **Left, right, top and bottom views**
- **Lowest common ancestor**
- **Root-to-leaf paths**
- **Parent pointers and distance K**
- **Serialise and deserialise**

## Mini project

Solve these 10 in 7 days, log time and pattern: Balanced Binary Tree, Diameter of Binary Tree, Binary Tree Right Side View, Top View of Binary Tree, Vertical Order Traversal, Lowest Common Ancestor of a Binary Tree, Path Sum III, All Nodes Distance K, Binary Tree Maximum Path Sum, Serialize and Deserialize Binary Tree.

## Check yourself

In Binary Tree Maximum Path Sum, what does the recursive function return to its parent, and what does it record in the global answer?

## Free resources

- [takeUforward tree series](https://takeuforward.org/) (takeUforward)
- [cp-algorithms: Lowest Common Ancestor](https://cp-algorithms.com/graph/lca.html) (cp-algorithms)
- [LeetCode problem set](https://leetcode.com/problemset/) (LeetCode)

Previous: [Binary Tree Traversals](https://www.internhack.in/roadmaps/tracks/dsa-placement/binary-trees) · Next: [Binary Search Trees](https://www.internhack.in/roadmaps/tracks/dsa-placement/bst)

## More in Trees, BSTs and Heaps

- [Binary Tree Traversals](https://www.internhack.in/roadmaps/tracks/dsa-placement/binary-trees)
- [Tree Problem Patterns](https://www.internhack.in/roadmaps/tracks/dsa-placement/tree-patterns)
- [Binary Search Trees](https://www.internhack.in/roadmaps/tracks/dsa-placement/bst)
- [Heaps and Priority Queues](https://www.internhack.in/roadmaps/tracks/dsa-placement/heaps)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DSA and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/dsa-placement)
